package com.ruicar.afs.cloud.apply.admin.manage.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.admin.service.SysUserRoleService;
import com.ruicar.afs.cloud.admin.service.SysUserService;
import com.ruicar.afs.cloud.apply.admin.auth.util.DataAccessUtil;
import com.ruicar.afs.cloud.apply.admin.manage.condition.ApplyUserCondition;
import com.ruicar.afs.cloud.apply.admin.manage.entity.*;
import com.ruicar.afs.cloud.apply.admin.manage.enums.ApplyAdminConfig;
import com.ruicar.afs.cloud.apply.admin.manage.feign.ApplyAdminUseChannelFeign;
import com.ruicar.afs.cloud.apply.admin.manage.mapper.ApplyDepartmentMapper;
import com.ruicar.afs.cloud.apply.admin.manage.mapper.ApplyUserMapper;
import com.ruicar.afs.cloud.apply.admin.manage.service.*;
import com.ruicar.afs.cloud.apply.admin.manage.vo.ApplyUserVo;
import com.ruicar.afs.cloud.apply.admin.manage.vo.ArchiveUserInfoVo;
import com.ruicar.afs.cloud.apply.admin.manage.vo.UserAuditVo;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.apply.enums.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * @author xudongdong
 * @date 2020-06-03
 */
@Slf4j
@AllArgsConstructor
@Service
public class ApplyUserServiceImpl extends ServiceImpl<ApplyUserMapper, SysUser> implements ApplyUserService {
    private final ApplyChannelBasicInfoService channelBasicInfoService;
    private final ApplyChannelWitnessInfoService channelWitnessInfoService;
    private final ApplyRoleService applyRoleService;
    private final ApplyUserRoleService userRoleService;
    private final ApplyChannelBasicInfoService applyChannelBasicInfoService;
    private final ApplyDeptAuthRegionService applyDeptAuthRegionService;
    private final ApplyDepartmentMapper applyDepartmentMapper;

    private final SysUserPowerService sysUserPowerService;

    private final ApplyUserTempService applyUserTempService;
    private final ApplyUserRoleTempService applyUserRoleTempService;
    private final ApplyAdminUseChannelFeign applyAdminUseChannelFeign;
    private final ApplyAdminConfig applyAdminConfig;
    private final ComAttachementFileService comAttachementFileService;

    @Override
    public List<SysRole> findRolesByUserId(Long id, CarTypeEnum businessType) {
        return baseMapper.listRolesByUserId(id, businessType);
    }

    @Override
    public List<SysRole> findRolesByUserIdTemp(Long id, CarTypeEnum businessType) {
        return baseMapper.listRolesByUserIdTemp(id, businessType);
    }

    @Override
    public List<String> findUserName(String userName) {
        return baseMapper.findUserName(userName);
    }

    @Override
    public List<SysChannelWitnessInfo> getWitnessList(String sp, String identityNumber) {
        return baseMapper.getWitnessList(sp, identityNumber);
    }

    @Override
    public void saveUser(ApplyUserCondition userCondition) {
        userCondition.setChannelId(DataAccessUtil.getChannelId());
        //用户校验
        this.check(userCondition);

        userCondition.setLockFlag(StatusEnum.NO.getValue());
        userCondition.setLastLoginFailTime(0L);
        userCondition.setLastPassChangeTime(0L);
        userCondition.setPassErrorCount(0);
        userCondition.setChannelId(DataAccessUtil.getChannelId());
        //初始密码
        userCondition.setPassword(SysUserService.ENCODER.encode(AfsEnumUtil.key(CreatUserEnum.DEFAULT_PWD)));
        if (!this.save(userCondition)) {
            throw new AfsBaseException(CommonConstants.FAIL, "添加失败！");
        }
        // 查询出来贷后的角色id
        SysRole loan = applyRoleService.getOne(Wrappers.<SysRole>query().lambda().eq(SysRole::getRoleCode, RoleEnum.ROLE_LOANAFTER.name()));
        // 查询佣金角色
        SysRole brokerage = applyRoleService.getOne(Wrappers.<SysRole>query().lambda().eq(SysRole::getRoleCode, RoleEnum.ROLE_COMMISSION.name()));
        //添加角色
        if (userCondition.getNewRoleIds() != null && userCondition.getNewRoleIds().length > 0) {
            for (Long roleId : userCondition.getNewRoleIds()) {

                // 如过角色id 是贷后或者佣金 也给二手车一个
                if (roleId.toString().equals(loan.getId().toString()) ) {
                    userRoleService.remove(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,userCondition.getUserId()).eq(SysUserRole::getRoleId,roleId));
                    // 说明有贷后
                    SysUserRole us = new SysUserRole();
                    us.setUserId(userCondition.getUserId());
                    us.setRoleId(roleId);
                    us.setBusinessType(CarTypeEnum.oldCar);
                    userRoleService.save(us);
                }
                if(roleId.toString().equals(brokerage.getId().toString())){
                    userRoleService.remove(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,userCondition.getUserId()).eq(SysUserRole::getRoleId,roleId));
                    // 说明有佣金
                    SysUserRole us = new SysUserRole();
                    us.setUserId(userCondition.getUserId());
                    us.setRoleId(roleId);
                    us.setBusinessType(CarTypeEnum.oldCar);
                    userRoleService.save(us);
                }
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userCondition.getUserId());
                ur.setRoleId(roleId);
                ur.setBusinessType(CarTypeEnum.newCar);
                userRoleService.save(ur);
            }
        }
        //添加角色
        if (userCondition.getOldRoleIds() != null && userCondition.getOldRoleIds().length > 0) {
            for (Long roleId : userCondition.getOldRoleIds()) {

                // 如过角色id 是贷后或者佣金 也给新车一个
                if (roleId.toString().equals(loan.getId().toString()) ) {
                    userRoleService.remove(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,userCondition.getUserId()).eq(SysUserRole::getRoleId,roleId));
                    SysUserRole us = new SysUserRole();
                    us.setUserId(userCondition.getUserId());
                    us.setRoleId(roleId);
                    us.setBusinessType(CarTypeEnum.newCar);
                    userRoleService.save(us);
                }
                if(roleId.toString().equals(brokerage.getId().toString())){
                    userRoleService.remove(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,userCondition.getUserId()).eq(SysUserRole::getRoleId,roleId));
                    SysUserRole us = new SysUserRole();
                    us.setUserId(userCondition.getUserId());
                    us.setRoleId(roleId);
                    us.setBusinessType(CarTypeEnum.newCar);
                    userRoleService.save(us);
                }
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userCondition.getUserId());
                ur.setRoleId(roleId);
                ur.setBusinessType(CarTypeEnum.oldCar);
                userRoleService.save(ur);
            }
        }
        // 查询新车 所有的进件角色
        List<Long> newIds =this.getNewApply();
        // 查询 二手车所有的进件角色
        List<Long> oldIds =this.getOldApply();
        // 新车进件权限
        List<SysUserRole> applyUserNewRole =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,userCondition.getUserId()).in(SysUserRole::getRoleId,newIds));
        // 二手车进件权限
        List<SysUserRole> applyUserOldRole =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,userCondition.getUserId()).in(SysUserRole::getRoleId,oldIds));
        // 新车账号权限
        List<SysUserRole> newUser =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,userCondition.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.newCar.toString()));
        // 二手车账号权限
        List<SysUserRole> oldUser =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,userCondition.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.oldCar.toString()));

        //部门新车权限
        List<SysDeptAuthRegion> newDept=applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda().eq(SysDeptAuthRegion::getDeptId,userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType,CarTypeEnum.newCar.toString()));
        // 部门二手车权限
        List<SysDeptAuthRegion> oldDept=applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda().eq(SysDeptAuthRegion::getDeptId,userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType,CarTypeEnum.oldCar.toString()));

        // 去查这个用户的
        // 这边去判断用户的权限
        SysUserPower power=sysUserPowerService.getOne(Wrappers.<SysUserPower>query().lambda().eq(SysUserPower::getUserId,userCondition.getUserId()));
        if(power==null){
            power =new SysUserPower();
            power.setUserId(userCondition.getUserId());
            power.setChannelId(userCondition.getChannelId());
            // 说明有新车
            if(applyUserNewRole.size()>0){
                power.setNewApplyPower(StatusEnum.NO.getValue());
            }else {
                power.setNewApplyPower(StatusEnum.NOTHING.getValue());
            }
            if(newUser.size()>0){
                if(newDept.size()>0){
                    power.setNewAccountPower(StatusEnum.NO.getValue());
                }else {
                    power.setNewAccountPower(StatusEnum.NOTHING.getValue());
                }

            }else {
                power.setNewAccountPower(StatusEnum.NOTHING.getValue());
            }
            if(applyUserOldRole.size()>0){
                power.setOdlApplyPower(StatusEnum.NO.getValue());
            }else {
                power.setOdlApplyPower(StatusEnum.NOTHING.getValue());
            }
            if(oldUser.size()>0){
                if(oldDept.size()>0){
                    power.setOldAccountPower(StatusEnum.NO.getValue());
                }else {
                    power.setOldAccountPower(StatusEnum.NOTHING.getValue());
                }

            }else {
                power.setOldAccountPower(StatusEnum.NOTHING.getValue());
            }

            power.setLockFlag(StatusEnum.NO.getValue());
            sysUserPowerService.save(power);
        }else {
            throw new AfsBaseException("添加进件权限信息有误！");
        }

        // 这边把数据加进临时用户表里
        SysUserTemp temp =new SysUserTemp();
        temp.setId(userCondition.getUserId());
        temp.setUsername(userCondition.getUsername());
        temp.setUserRealName(userCondition.getUserRealName());
        temp.setUserEmail(userCondition.getUserEmail());
        temp.setPassword(null);
        temp.setPhone(userCondition.getPhone());
        temp.setDeptId(userCondition.getDeptId());
        temp.setChannelId(userCondition.getChannelId());
        temp.setIdentityNumber(userCondition.getIdentityNumber());
        if(StatusEnum.NO.getValue().toString().equals(power.getNewAccountPower())){
            temp.setNewStatus(StatusEnum.APPROVE.getValue().toString());
        }
        if(StatusEnum.NO.getValue().toString().equals(power.getOldAccountPower())){
            temp.setOldStatus(StatusEnum.APPROVE.getValue().toString());
        }
        temp.setLockFlag(StatusEnum.YES.getValue().toString());
        temp.setCreateBy(SecurityUtils.getUsername());
        temp.setCreateTime(new Date(System.currentTimeMillis()));
        applyUserTempService.save(temp);

    }


    @Override
    public IResponse<Boolean> saveUserTemp(ApplyUserCondition userCondition) {
        userCondition.setChannelId(DataAccessUtil.getChannelId());
        //用户校验
        this.checkTemp(userCondition);
        SysUserTemp userTemp=new SysUserTemp();
        userTemp.setUsername(userCondition.getUsername());
        userTemp.setUserRealName(userCondition.getUserRealName());
        userTemp.setUserEmail(userCondition.getUserEmail());
        userTemp.setPhone(userCondition.getPhone());
        userTemp.setDeptId(userCondition.getDeptId());
        userTemp.setIdentityNumber(userCondition.getIdentityNumber());
        userTemp.setChannelId(DataAccessUtil.getChannelId());
        if (!applyUserTempService.save(userTemp)) {
            throw new AfsBaseException(CommonConstants.FAIL, "添加失败！");
        }
        SysUserTemp tempUser=applyUserTempService.getOne(Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getUsername,userCondition.getUsername()));

        //  去存储图片信息
        ComAttachementFile comAttachmentFile = new ComAttachementFile();
        comAttachmentFile.setBelongNo(tempUser.getId().toString());
        comAttachmentFile.setBusiNo(tempUser.getId().toString());
        comAttachmentFile.setAttachmentCode(userCondition.getFinalimgList().get(0).getType());
        comAttachmentFile.setAttachmentName(NodeTypeEnum.IDENTITY_CARD.getValue());
        comAttachmentFile.setFileStatus(StatusEnum.DRAFT.getValue());
        comAttachmentFile.setFileType(userCondition.getFinalimgList().get(0).getSuffix());
        comAttachmentFile.setFileId(userCondition.getFinalimgList().get(0).getMd5());
        comAttachmentFile.setHistoryVersion("v0");
        comAttachmentFile.setFileName(userCondition.getFinalimgList().get(0).getFileName());
        comAttachmentFile.setUploadTime(new Date());
        comAttachmentFile.setCreateBy(SecurityUtils.getUsername());
        //存到file表
        comAttachementFileService.save(comAttachmentFile);

        // 查询出来贷后的角色id
        SysRole loan = applyRoleService.getOne(Wrappers.<SysRole>query().lambda().eq(SysRole::getRoleCode, RoleEnum.ROLE_LOANAFTER.name()));
        // 查询佣金角色
        SysRole brokerage = applyRoleService.getOne(Wrappers.<SysRole>query().lambda().eq(SysRole::getRoleCode, RoleEnum.ROLE_COMMISSION.name()));
        //添加角色
        if (userCondition.getNewRoleIds() != null && userCondition.getNewRoleIds().length > 0) {
            for (Long roleId : userCondition.getNewRoleIds()) {
                // 如过角色id 是贷后或者佣金 也给二手车一个
                if (roleId.toString().equals(loan.getId().toString()) ) {
                    applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getRoleId,roleId));
                    // 说明有贷后
                    SysUserRoleTemp us = new SysUserRoleTemp();
                    us.setUserId(tempUser.getId());
                    us.setRoleId(roleId);
                    us.setBusinessType(CarTypeEnum.oldCar);
                    applyUserRoleTempService.save(us);
                }
                if(roleId.toString().equals(brokerage.getId().toString())){
                    applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getRoleId,roleId));
                    // 说明有佣金
                    SysUserRoleTemp us = new SysUserRoleTemp();
                    us.setUserId(tempUser.getId());
                    us.setRoleId(roleId);
                    us.setBusinessType(CarTypeEnum.oldCar);
                    applyUserRoleTempService.save(us);
                }
                SysUserRoleTemp ur = new SysUserRoleTemp();
                ur.setUserId(tempUser.getId());
                ur.setRoleId(roleId);
                ur.setBusinessType(CarTypeEnum.newCar);
                applyUserRoleTempService.save(ur);
            }
        }
        //添加角色
        if (userCondition.getOldRoleIds() != null && userCondition.getOldRoleIds().length > 0) {
            for (Long roleId : userCondition.getOldRoleIds()) {
                // 如过角色id 是贷后或者佣金 也给新车一个
                if (roleId.toString().equals(loan.getId().toString()) ) {
                    applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getRoleId,roleId));
                    SysUserRoleTemp us = new SysUserRoleTemp();
                    us.setUserId(tempUser.getId());
                    us.setRoleId(roleId);
                    us.setBusinessType(CarTypeEnum.newCar);
                    applyUserRoleTempService.save(us);
                }
                if(roleId.toString().equals(brokerage.getId().toString())){
                    applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getRoleId,roleId));
                    SysUserRoleTemp us = new SysUserRoleTemp();
                    us.setUserId(tempUser.getId());
                    us.setRoleId(roleId);
                    us.setBusinessType(CarTypeEnum.newCar);
                    applyUserRoleTempService.save(us);
                }
                SysUserRoleTemp ur = new SysUserRoleTemp();
                ur.setUserId(tempUser.getId());
                ur.setRoleId(roleId);
                ur.setBusinessType(CarTypeEnum.oldCar);
                applyUserRoleTempService.save(ur);
            }
        }
        // 查询新车 所有的进件角色
        List<Long> newIds =this.getNewApply();
        // 查询 二手车所有的进件角色
        List<Long> oldIds =this.getOldApply();
        // 新车进件权限
        List<SysUserRoleTemp> applyUserNewRole =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).in(SysUserRoleTemp::getRoleId,newIds));
        // 二手车进件权限
        List<SysUserRoleTemp> applyUserOldRole =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).in(SysUserRoleTemp::getRoleId,oldIds));
        // 新车账号权限
        List<SysUserRoleTemp> newUser =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getBusinessType,CarTypeEnum.newCar.toString()));
        // 二手车账号权限
        List<SysUserRoleTemp> oldUser =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getBusinessType,CarTypeEnum.oldCar.toString()));

        //部门新车权限
        List<SysDeptAuthRegion> newDept=applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda().eq(SysDeptAuthRegion::getDeptId,userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType,CarTypeEnum.newCar.toString()));
        // 部门二手车权限
        List<SysDeptAuthRegion> oldDept=applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda().eq(SysDeptAuthRegion::getDeptId,userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType,CarTypeEnum.oldCar.toString()));

        // 获取管理员的权限
        SysUserPower adminPower=sysUserPowerService.getOne(Wrappers.<SysUserPower>query().lambda().eq(SysUserPower::getChannelId,DataAccessUtil.getChannelId()).eq(SysUserPower::getUserManage,StatusEnum.YES.getValue().toString()));
        if(adminPower==null){
            throw new AfsBaseException("获取管理员信息失败！");
        }

        // 去查这个用户的
        // 这边去判断用户的权限
        SysUserPower power=sysUserPowerService.getOne(Wrappers.<SysUserPower>query().lambda().eq(SysUserPower::getUserId,tempUser.getId()));
        if(power==null){
            power =new SysUserPower();
            power.setUserId(tempUser.getId());
            power.setChannelId(tempUser.getChannelId());
            // 说明有新车
            if(applyUserNewRole.size()>0){
                if(adminPower.getNewApplyPower().toString().equals(StatusEnum.NO.getValue().toString())){
                    power.setNewApplyPower(StatusEnum.NO.getValue());
                }else {
                    throw new AfsBaseException("暂无新车进件权限！");
                }
            }else {
                power.setNewApplyPower(StatusEnum.NOTHING.getValue());
            }
            if(newUser.size()>0){
                if(newDept.size()>0){
                    if(adminPower.getNewAccountPower().toString().equals(StatusEnum.NO.getValue().toString())){
                        power.setNewAccountPower(StatusEnum.NO.getValue());
                    }else {
                        throw new AfsBaseException("暂无新车账户权限！");
                    }
                }else {
                    power.setNewAccountPower(StatusEnum.NOTHING.getValue());
                }

            }else {
                power.setNewAccountPower(StatusEnum.NOTHING.getValue());
            }
            if(applyUserOldRole.size()>0){
                if(adminPower.getOdlApplyPower().toString().equals(StatusEnum.NO.getValue().toString())){
                    power.setOdlApplyPower(StatusEnum.NO.getValue());
                }else {
                    throw new AfsBaseException("暂无二手车进件权限！");
                }
            }else {
                power.setOdlApplyPower(StatusEnum.NOTHING.getValue());
            }
            if(oldUser.size()>0){
                if(oldDept.size()>0){
                    if(adminPower.getOldAccountPower().toString().equals(StatusEnum.NO.getValue().toString())){
                        power.setOldAccountPower(StatusEnum.NO.getValue());
                    }else {
                        throw new AfsBaseException("暂无二手车账户权限！");
                    }
                }else {
                    power.setOldAccountPower(StatusEnum.NOTHING.getValue());
                }

            }else {
                power.setOldAccountPower(StatusEnum.NOTHING.getValue());
            }

            power.setLockFlag(StatusEnum.NO.getValue());
        }else {
            throw new AfsBaseException("添加进件权限信息有误！");
        }


        if(StatusEnum.NO.getValue().toString().equals(power.getNewAccountPower())){
            userTemp.setNewStatus(StatusEnum.APPROVE.getValue().toString());
        }
        if(StatusEnum.NO.getValue().toString().equals(power.getOldAccountPower())){
            userTemp.setOldStatus(StatusEnum.APPROVE.getValue().toString());
        }
        tempUser.setLockFlag(StatusEnum.YES.getValue().toString());
        tempUser.setCreateBy(SecurityUtils.getUsername());
        tempUser.setCreateTime(new Date(System.currentTimeMillis()));
        applyUserTempService.update(tempUser,Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getUsername,userCondition.getUsername()));

        UserAuditVo vo=new UserAuditVo();
        // 默认需要二个人都同意
        String sign=CommonConstants.STATUS_NORMAL;

        List<SysUserRoleTemp> tempList =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()));
        if(tempList.size()>0){
            vo.setSysUserRoleTemp(tempList);
        }else {
            throw new AfsBaseException("获取用户角色信息失败！");
        }
        // 声明一个值去判断是不是只有佣金和贷后
        String only=CommonConstants.STATUS_ERROR;
        for (SysUserRoleTemp temp:tempList){
            // 先声明默认
            if(!loan.getId().toString().equals(temp.getRoleId().toString())){
                if(!brokerage.getId().toString().equals(temp.getRoleId().toString())){
                    only=CommonConstants.STATUS_NORMAL;
                }
            }
        }
        if(StatusEnum.NO.getValue().toString().equals(power.getNewAccountPower())&&StatusEnum.NO.getValue().toString().equals(power.getOldAccountPower().toString())){
            //  说明新车二手车都有，那么需要判断是不是只有佣金贷后
            if(CommonConstants.STATUS_ERROR.equals(only)){
                sign=CommonConstants.STATUS_ERROR;
            }
        }



        SysDepartment department =applyDepartmentMapper.selectById(tempUser.getDeptId());
        if(StatusEnum.NO.getValue().toString().equals(power.getNewAccountPower())){
            //  说明我是新车  我要发起新车的审核
            ApplyUserVo nUser =new ApplyUserVo();
            nUser.setBusinessType(CarTypeEnum.newCar.toString());
            nUser.setChannelId(tempUser.getChannelId());
            if(department==null){
                throw new AfsBaseException("获取部门信息失败！");
            }else {
                nUser.setDepartmentName(department.getTitle());
            }
            nUser.setPhone(tempUser.getPhone());
            nUser.setIdentityNumber(tempUser.getIdentityNumber());
            nUser.setUserEmail(tempUser.getUserEmail());
            nUser.setUserId(tempUser.getId());
            nUser.setUsername(tempUser.getUsername());
            nUser.setUserRealName(tempUser.getUserRealName());
            nUser.setDeptId(tempUser.getDeptId());
            nUser.setSign(sign);
            vo.setNewUser(nUser);
            vo.setComAttachementFile(comAttachmentFile);
            tempUser.setNewStatus(StatusEnum.APPROVE.getValue());
        }
        if(StatusEnum.NO.getValue().toString().equals(power.getOldAccountPower())){
            // 说明我是二手车  我要发起二手车审核
            ApplyUserVo oUser =new ApplyUserVo();
            oUser.setBusinessType(CarTypeEnum.oldCar.toString());
            oUser.setChannelId(tempUser.getChannelId());
            if(department==null){
                throw new AfsBaseException("获取部门信息失败！");
            }else {
                oUser.setDepartmentName(department.getTitle());
            }
            oUser.setPhone(tempUser.getPhone());
            oUser.setIdentityNumber(tempUser.getIdentityNumber());
            oUser.setUserEmail(tempUser.getUserEmail());
            oUser.setUserId(tempUser.getId());
            oUser.setUsername(tempUser.getUsername());
            oUser.setDeptId(tempUser.getDeptId());
            oUser.setUserRealName(tempUser.getUserRealName());
            oUser.setSign(sign);
            vo.setOldUser(oUser);
            vo.setComAttachementFile(comAttachmentFile);
            tempUser.setOldStatus(StatusEnum.APPROVE.getValue());
        }

        applyUserTempService.update(tempUser,Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getId,tempUser.getId()));
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("clientId", applyAdminConfig.getChannelClientId());
        requestHeader.put("clientSecret", applyAdminConfig.getChannelClientSecret());
        IResponse bb =applyAdminUseChannelFeign.addApplyUser(vo,requestHeader);
        if(!CommonConstants.SUCCESS.toString().equals(bb.getCode())){
            throw new AfsBaseException("创建用户失败!");
        }
        // 发送给渠道
        return IResponse.success(Boolean.TRUE).setMsg("创建成功！");


    }

    @Override
    public IResponse<Boolean> editUserTemp(ApplyUserCondition userCondition) {
        //  去存储图片信息
        ComAttachementFile comAttachmentFile = new ComAttachementFile();
        comAttachmentFile.setBelongNo(userCondition.getUserId().toString());
        comAttachmentFile.setBusiNo(userCondition.getUserId().toString());
        comAttachmentFile.setAttachmentCode(userCondition.getFinalimgList().get(0).getType());
        comAttachmentFile.setAttachmentName(NodeTypeEnum.IDENTITY_CARD.getValue());
        comAttachmentFile.setFileStatus(StatusEnum.DRAFT.getValue());
        comAttachmentFile.setFileType(userCondition.getFinalimgList().get(0).getSuffix());
        comAttachmentFile.setFileId(userCondition.getFinalimgList().get(0).getMd5());
        comAttachmentFile.setHistoryVersion("v0");
        comAttachmentFile.setFileName(userCondition.getFinalimgList().get(0).getFileName());
        comAttachmentFile.setUploadTime(new Date());
        comAttachmentFile.setCreateBy(SecurityUtils.getUsername());
        //存到file表
        comAttachementFileService.save(comAttachmentFile);
        userCondition.setChannelId(DataAccessUtil.getChannelId());
        // 先移除现在所有的角色
        applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,userCondition.getUserId()));
        // 获取这个部门能做新车还是二手车  还是都可以
        List<SysDeptAuthRegion> newCarRegion = applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda()
                .eq(SysDeptAuthRegion::getDeptId, userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType, CarTypeEnum.newCar));

        List<SysDeptAuthRegion> oldCarRegion = applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda()
                .eq(SysDeptAuthRegion::getDeptId, userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType, CarTypeEnum.oldCar));
        // 去校验
        this.checkTemp(userCondition);
        // 查询我临时表的用户信息
        SysUserTemp tempUser=applyUserTempService.getOne(Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getUsername,userCondition.getUsername()));
        tempUser.setDeptId(userCondition.getDeptId());
        tempUser.setUserRealName(userCondition.getUserRealName());
        tempUser.setPhone(userCondition.getPhone());
        tempUser.setUserEmail(userCondition.getUserEmail());
        applyUserTempService.update(tempUser,Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getUsername,userCondition.getUsername()));
        // 查询出来贷后的角色id
        SysRole loan = applyRoleService.getOne(Wrappers.<SysRole>query().lambda().eq(SysRole::getRoleCode, RoleEnum.ROLE_LOANAFTER.name()));
        // 查询佣金角色
        SysRole brokerage = applyRoleService.getOne(Wrappers.<SysRole>query().lambda().eq(SysRole::getRoleCode, RoleEnum.ROLE_COMMISSION.name()));
        //添加角色
        if(newCarRegion.size()>0){
            if (userCondition.getNewRoleIds() != null && userCondition.getNewRoleIds().length > 0) {
                for (Long roleId : userCondition.getNewRoleIds()) {

                    // 如过角色id 是贷后或者佣金 也给二手车一个
                    if (roleId.toString().equals(loan.getId().toString()) ) {
                        applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getRoleId,roleId));
                        // 说明有贷后
                        SysUserRoleTemp us = new SysUserRoleTemp();
                        us.setUserId(tempUser.getId());
                        us.setRoleId(roleId);
                        us.setBusinessType(CarTypeEnum.oldCar);
                        applyUserRoleTempService.save(us);
                    }
                    if(roleId.toString().equals(brokerage.getId().toString())){
                        applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getRoleId,roleId));
                        // 说明有佣金
                        SysUserRoleTemp us = new SysUserRoleTemp();
                        us.setUserId(tempUser.getId());
                        us.setRoleId(roleId);
                        us.setBusinessType(CarTypeEnum.oldCar);
                        applyUserRoleTempService.save(us);
                    }
                    SysUserRoleTemp ur = new SysUserRoleTemp();
                    ur.setUserId(tempUser.getId());
                    ur.setRoleId(roleId);
                    ur.setBusinessType(CarTypeEnum.newCar);
                    applyUserRoleTempService.save(ur);
                }
            }
        }
        //添加角色
        if(oldCarRegion.size()>0){
            if (userCondition.getOldRoleIds() != null && userCondition.getOldRoleIds().length > 0) {
                for (Long roleId : userCondition.getOldRoleIds()) {

                    // 如过角色id 是贷后或者佣金 也给新车一个
                    if (roleId.toString().equals(loan.getId().toString()) ) {
                        applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getRoleId,roleId));
                        SysUserRoleTemp us = new SysUserRoleTemp();
                        us.setUserId(tempUser.getId());
                        us.setRoleId(roleId);
                        us.setBusinessType(CarTypeEnum.newCar);
                        applyUserRoleTempService.save(us);
                    }
                    if(roleId.toString().equals(brokerage.getId().toString())){
                        applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getRoleId,roleId));
                        SysUserRoleTemp us = new SysUserRoleTemp();
                        us.setUserId(tempUser.getId());
                        us.setRoleId(roleId);
                        us.setBusinessType(CarTypeEnum.newCar);
                        applyUserRoleTempService.save(us);
                    }
                    SysUserRoleTemp ur = new SysUserRoleTemp();
                    ur.setUserId(tempUser.getId());
                    ur.setRoleId(roleId);
                    ur.setBusinessType(CarTypeEnum.oldCar);
                    applyUserRoleTempService.save(ur);
                }
            }
        }
        // 查询新车 所有的进件角色
        List<Long> newIds =this.getNewApply();
        // 查询 二手车所有的进件角色
        List<Long> oldIds =this.getOldApply();
        // 新车进件权限
        List<SysUserRoleTemp> applyUserNewRole =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).in(SysUserRoleTemp::getRoleId,newIds));
        // 二手车进件权限
        List<SysUserRoleTemp> applyUserOldRole =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).in(SysUserRoleTemp::getRoleId,oldIds));
        // 新车账号权限
        List<SysUserRoleTemp> newUser =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getBusinessType,CarTypeEnum.newCar.toString()));
        // 二手车账号权限
        List<SysUserRoleTemp> oldUser =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getBusinessType,CarTypeEnum.oldCar.toString()));

        //部门新车权限
        List<SysDeptAuthRegion> newDept=applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda().eq(SysDeptAuthRegion::getDeptId,userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType,CarTypeEnum.newCar.toString()));
        // 部门二手车权限
        List<SysDeptAuthRegion> oldDept=applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda().eq(SysDeptAuthRegion::getDeptId,userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType,CarTypeEnum.oldCar.toString()));

        // 获取管理员的权限
        SysUserPower adminPower=sysUserPowerService.getOne(Wrappers.<SysUserPower>query().lambda().eq(SysUserPower::getChannelId,DataAccessUtil.getChannelId()).eq(SysUserPower::getUserManage,StatusEnum.YES.getValue().toString()));
        if(adminPower==null){
            throw new AfsBaseException("获取管理员信息失败！");
        }

        // 去查这个用户的
        // 这边去判断用户的权限
        SysUserPower power=sysUserPowerService.getOne(Wrappers.<SysUserPower>query().lambda().eq(SysUserPower::getUserId,tempUser.getId()));
        if(power==null){
            SysUser uu=this.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUserId,userCondition.getUserId()));
            if(uu==null){
                power =new SysUserPower();
                power.setUserId(tempUser.getId());
                power.setChannelId(tempUser.getChannelId());
                // 说明有新车
                if(applyUserNewRole.size()>0){
                    if(adminPower.getNewApplyPower().toString().equals(StatusEnum.NO.getValue().toString())){
                        power.setNewApplyPower(StatusEnum.NO.getValue());
                    }else {
                        throw new AfsBaseException("暂无新车进件权限！");
                    }
                }else {
                    power.setNewApplyPower(StatusEnum.NOTHING.getValue());
                }
                if(newUser.size()>0){
                    if(newDept.size()>0){
                        if(adminPower.getNewAccountPower().toString().equals(StatusEnum.NO.getValue().toString())){
                            power.setNewAccountPower(StatusEnum.NO.getValue());
                        }else {
                            throw new AfsBaseException("暂无新车账户权限！");
                        }
                    }else {
                        power.setNewAccountPower(StatusEnum.NOTHING.getValue());
                    }

                }else {
                    power.setNewAccountPower(StatusEnum.NOTHING.getValue());
                }
                if(applyUserOldRole.size()>0){
                    if(adminPower.getOdlApplyPower().toString().equals(StatusEnum.NO.getValue().toString())){
                        power.setOdlApplyPower(StatusEnum.NO.getValue());
                    }else {
                        throw new AfsBaseException("暂无二手车进件权限！");
                    }
                }else {
                    power.setOdlApplyPower(StatusEnum.NOTHING.getValue());
                }
                if(oldUser.size()>0){
                    if(oldDept.size()>0){
                        if(adminPower.getOldAccountPower().toString().equals(StatusEnum.NO.getValue().toString())){
                            power.setOldAccountPower(StatusEnum.NO.getValue());
                        }else {
                            throw new AfsBaseException("暂无二手车账户权限！");
                        }
                    }else {
                        power.setOldAccountPower(StatusEnum.NOTHING.getValue());
                    }

                }else {
                    power.setOldAccountPower(StatusEnum.NOTHING.getValue());
                }

                power.setLockFlag(StatusEnum.NO.getValue());
            }else {
                throw new AfsBaseException("获取用户权限失败！");
            }
        }else {
            // 说明有新车
            if(applyUserNewRole.size()>0){
                if(adminPower.getNewApplyPower().toString().equals(StatusEnum.NO.getValue().toString())){
                    if(StatusEnum.YES.getValue().toString().equals(power.getNewApplyPower())){
                        throw new AfsBaseException("暂无新车进件权限！");
                    }else {
                        power.setNewApplyPower(StatusEnum.NO.getValue());
                    }
                }else {
                    throw new AfsBaseException("暂无新车进件权限！");
                }
            }else {
                power.setNewApplyPower(StatusEnum.NOTHING.getValue());
            }
            if(newUser.size()>0){
                if(newDept.size()>0){
                    if(adminPower.getNewAccountPower().toString().equals(StatusEnum.NO.getValue().toString())){
                        if(StatusEnum.YES.getValue().toString().equals(power.getNewAccountPower())){
                            throw new AfsBaseException("用户暂无新车账户权限！");
                        }else {
                            power.setNewAccountPower(StatusEnum.NO.getValue());
                        }
                    }else {
                        throw new AfsBaseException("暂无新车账户权限！");
                    }
                }else {
                    power.setNewAccountPower(StatusEnum.NOTHING.getValue());
                }

            }else {
                power.setNewAccountPower(StatusEnum.NOTHING.getValue());
            }
            if(applyUserOldRole.size()>0){
                if(adminPower.getOdlApplyPower().toString().equals(StatusEnum.NO.getValue().toString())){
                    if(StatusEnum.YES.getValue().toString().equals(power.getOdlApplyPower())){
                        throw new AfsBaseException("暂无二手车进件权限！");
                    }else {
                        power.setOdlApplyPower(StatusEnum.NO.getValue());
                    }
                }else {
                    throw new AfsBaseException("暂无二手车进件权限！");
                }
            }else {
                power.setOdlApplyPower(StatusEnum.NOTHING.getValue());
            }
            if(oldUser.size()>0){
                if(oldDept.size()>0){
                    if(adminPower.getOldAccountPower().toString().equals(StatusEnum.NO.getValue().toString())){
                        if(StatusEnum.YES.getValue().toString().equals(power.getOldAccountPower())){
                            throw new AfsBaseException("用户暂无二手车账户权限！");
                        }else {
                            power.setOldAccountPower(StatusEnum.NO.getValue());
                        }
                    }else {
                        throw new AfsBaseException("暂无二手车账户权限！");
                    }
                }else {
                    power.setOldAccountPower(StatusEnum.NOTHING.getValue());
                }

            }else {
                power.setOldAccountPower(StatusEnum.NOTHING.getValue());
            }
            sysUserPowerService.update(power,Wrappers.<SysUserPower>query().lambda().eq(SysUserPower::getUserId,userCondition.getUserId()));
        }

        // 接下来我要去看看 对比新车的变化
        List<SysUserRole> formerNewRole=userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,userCondition.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.newCar.toString()));
        List<Long> formerNewRoleIds =new ArrayList<>();
        for (SysUserRole role:formerNewRole){
            formerNewRoleIds.add(role.getRoleId());
        }

        List<SysUserRoleTemp> newRole=applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,userCondition.getUserId()).eq(SysUserRoleTemp::getBusinessType,CarTypeEnum.newCar.toString()));
        List<Long> newRoleIds =new ArrayList<>();
        for (SysUserRoleTemp role:newRole){
            newRoleIds.add(role.getRoleId());
        }

        List<SysUserRole> formerOldRole=userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,userCondition.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.oldCar.toString()));
        List<Long> formerOldRoleIds =new ArrayList<>();
        for (SysUserRole role :formerOldRole){
            formerOldRoleIds.add(role.getRoleId());
        }

        List<SysUserRoleTemp> oldRole=applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,userCondition.getUserId()).eq(SysUserRoleTemp::getBusinessType,CarTypeEnum.oldCar.toString()));
        List<Long> oldRoleIds =new ArrayList<>();
        for (SysUserRoleTemp role :oldRole){
            oldRoleIds.add(role.getRoleId());
        }
        Boolean compareNewCar=this.checkDiffrent(newRoleIds,formerNewRoleIds);
        Boolean compareOldCar=this.checkDiffrent(oldRoleIds,formerOldRoleIds);
        Boolean manage =true;

        // 比较基础信息
        SysUser formerUser =this.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUserId,userCondition.getUserId()));

        SysUserTemp userTemp =applyUserTempService.getOne(Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getId,userCondition.getUserId()));
        if(formerUser==null){
            UserAuditVo vo=new UserAuditVo();
            // 默认需要二个人都同意
            String sign=CommonConstants.STATUS_NORMAL;

            List<SysUserRoleTemp> tempList =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()));
            if(tempList.size()>0){
                vo.setSysUserRoleTemp(tempList);
            }else {
                throw new AfsBaseException("获取用户角色信息失败！");
            }
            // 说明之前没有是全拒绝
            // 声明一个值去判断是不是只有佣金和贷后
            String only=CommonConstants.STATUS_ERROR;
            for (SysUserRoleTemp temp:tempList){
                // 先声明默认
                if(!loan.getId().toString().equals(temp.getRoleId().toString())){
                    if(!brokerage.getId().toString().equals(temp.getRoleId().toString())){
                        only=CommonConstants.STATUS_NORMAL;
                    }
                }
            }
            if(StatusEnum.NO.getValue().toString().equals(power.getNewAccountPower())&&StatusEnum.NO.getValue().toString().equals(power.getOldAccountPower().toString())){
                //  说明新车二手车都有，那么需要判断是不是只有佣金贷后
                if(CommonConstants.STATUS_ERROR.equals(only)){
                    sign=CommonConstants.STATUS_ERROR;
                }
            }



            SysDepartment department =applyDepartmentMapper.selectById(tempUser.getDeptId());
            if(StatusEnum.NO.getValue().toString().equals(power.getNewAccountPower())){
                //  说明我是新车  我要发起新车的审核
                ApplyUserVo nUser =new ApplyUserVo();
                nUser.setBusinessType(CarTypeEnum.newCar.toString());
                nUser.setChannelId(tempUser.getChannelId());
                if(department==null){
                    throw new AfsBaseException("获取部门信息失败！");
                }else {
                    nUser.setDepartmentName(department.getTitle());
                }
                nUser.setPhone(tempUser.getPhone());
                nUser.setIdentityNumber(tempUser.getIdentityNumber());
                nUser.setUserEmail(tempUser.getUserEmail());
                nUser.setUserId(tempUser.getId());
                nUser.setUsername(tempUser.getUsername());
                nUser.setUserRealName(tempUser.getUserRealName());
                nUser.setSign(sign);
                vo.setNewUser(nUser);
                vo.setComAttachementFile(comAttachmentFile);
                tempUser.setNewStatus(StatusEnum.APPROVE.getValue());
            }
            if(StatusEnum.NO.getValue().toString().equals(power.getOldAccountPower())){
                // 说明我是二手车  我要发起二手车审核
                ApplyUserVo oUser =new ApplyUserVo();
                oUser.setBusinessType(CarTypeEnum.oldCar.toString());
                oUser.setChannelId(tempUser.getChannelId());
                if(department==null){
                    throw new AfsBaseException("获取部门信息失败！");
                }else {
                    oUser.setDepartmentName(department.getTitle());
                }
                oUser.setPhone(tempUser.getPhone());
                oUser.setIdentityNumber(tempUser.getIdentityNumber());
                oUser.setUserEmail(tempUser.getUserEmail());
                oUser.setUserId(tempUser.getId());
                oUser.setUsername(tempUser.getUsername());
                oUser.setUserRealName(tempUser.getUserRealName());
                oUser.setSign(sign);
                vo.setOldUser(oUser);
                vo.setComAttachementFile(comAttachmentFile);
                tempUser.setOldStatus(StatusEnum.APPROVE.getValue());
            }

            applyUserTempService.update(tempUser,Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getId,tempUser.getId()));
            Map<String, String> requestHeader = new HashMap<>();
            requestHeader.put("clientId", applyAdminConfig.getChannelClientId());
            requestHeader.put("clientSecret", applyAdminConfig.getChannelClientSecret());
            IResponse bb =applyAdminUseChannelFeign.addApplyUser(vo,requestHeader);
            if(!CommonConstants.SUCCESS.toString().equals(bb.getCode())){
                throw new AfsBaseException("修改失败!");
            }
            return IResponse.success(Boolean.TRUE).setMsg("操作成功！");
        }
        if(!formerUser.getDeptId().equals(userTemp.getDeptId())){
            manage=false;
        }
        if(!formerUser.getPhone().equals(userTemp.getPhone())){
            manage=false;
        }
        if(!formerUser.getUserEmail().equals(userTemp.getUserEmail())){
            manage=false;
        }
        if(!formerUser.getUserRealName().equals(userTemp.getUserRealName())){
            manage=false;
        }
        if(StatusEnum.YES.getValue().toString().equals(userTemp.getLockFlag())){
            manage=false;
        }

        //先查询我的影像件数据
        List<ComAttachementFile> fileList=comAttachementFileService.list(Wrappers.<ComAttachementFile>query().lambda().eq(ComAttachementFile::getBusiNo,userCondition.getUserId().toString()).eq(ComAttachementFile::getFileStatus,StatusEnum.STANDARD.getValue().toString()));
        if(fileList.size()>0){
            if(fileList.get(0).getFileId().equals(userCondition.getFinalimgList().get(0).getMd5())){

            }else {
                manage=false;
            }
        }else {
            List<ComAttachementFile> file=comAttachementFileService.list(Wrappers.<ComAttachementFile>query().lambda().eq(ComAttachementFile::getBusiNo,userCondition.getUserId().toString()));
            if(file.size()>1){
                manage=false;
            }
        }

        // 先去查看下现在自己的部门是新车还是二手车


        if(manage==false){
            String sign=CommonConstants.STATUS_NORMAL;
            // 说明基础信息被修改，需要二边都发申请
            UserAuditVo vo=new UserAuditVo();
            List<SysUserRoleTemp> tempList =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()));

            List<SysUserRole> list =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,tempUser.getId()));
            if(tempList.size()>0&&list.size()>0){
                vo.setSysUserRoleTemp(tempList);
                vo.setSysUserRole(list);
            }else {
                throw new AfsBaseException("获取用户角色信息失败！");
            }
            // 声明一个值去判断是不是只有佣金和贷后
            String only=CommonConstants.STATUS_ERROR;
            for (SysUserRoleTemp temp:tempList){
                // 先声明默认
                if(!loan.getId().toString().equals(temp.getRoleId().toString())){
                    if(!brokerage.getId().toString().equals(temp.getRoleId().toString())){
                        only=CommonConstants.STATUS_NORMAL;
                    }
                }
            }
            if(newRole.size()>0&&oldRole.size()>0){
                //  说明新车二手车都有，那么需要判断是不是只有佣金贷后
                if(CommonConstants.STATUS_ERROR.equals(only)){
                    sign=CommonConstants.STATUS_ERROR;
                }
            }



            SysDepartment department =applyDepartmentMapper.selectById(tempUser.getDeptId());

            SysDepartment formorDepartment =applyDepartmentMapper.selectById(formerUser.getDeptId());
            if(newCarRegion.size()>0){
                if(newRole.size()>0){
                    //  说明我是新车  我要发起新车的审核
                    ApplyUserVo nUser =new ApplyUserVo();
                    nUser.setBusinessType(CarTypeEnum.newCar.toString());
                    nUser.setChannelId(tempUser.getChannelId());
                    if(department==null||formorDepartment==null){
                        throw new AfsBaseException("获取部门信息失败！");
                    }else {
                        nUser.setDepartmentName(department.getTitle());
                        nUser.setFormerDepartmentName(formorDepartment.getTitle());
                    }
                    nUser.setPhone(tempUser.getPhone());
                    nUser.setFormerPhone(formerUser.getPhone());
                    nUser.setIdentityNumber(tempUser.getIdentityNumber());
                    nUser.setUserEmail(tempUser.getUserEmail());
                    nUser.setFormerUserEmail(formerUser.getUserEmail());
                    nUser.setUserId(tempUser.getId());
                    nUser.setUsername(tempUser.getUsername());
                    nUser.setUserRealName(tempUser.getUserRealName());
                    nUser.setFormerUserRealName(formerUser.getUserRealName());
                    nUser.setDeptId(tempUser.getDeptId());
                    nUser.setFormerDeptId(formerUser.getDeptId());
                    nUser.setSign(sign);
                    vo.setNewUser(nUser);
                    vo.setComAttachementFile(comAttachmentFile);
                    if(tempUser.getNewStatus()==null||tempUser.getNewStatus()==""){
                        tempUser.setNewStatus(StatusEnum.APPROVE.getValue());
                    }else {
                        tempUser.setNewStatus(StatusEnum.REEXAMINE.getValue());
                    }

                }
            }
            if(oldCarRegion.size()>0){
                if(oldRole.size()>0){
                    // 说明我是二手车  我要发起二手车审核
                    ApplyUserVo oUser =new ApplyUserVo();
                    oUser.setBusinessType(CarTypeEnum.oldCar.toString());
                    oUser.setChannelId(tempUser.getChannelId());
                    if(department==null){
                        throw new AfsBaseException("获取部门信息失败！");
                    }else {
                        oUser.setDepartmentName(department.getTitle());
                        oUser.setFormerDepartmentName(formorDepartment.getTitle());
                    }
                    oUser.setPhone(tempUser.getPhone());
                    oUser.setFormerPhone(formerUser.getPhone());
                    oUser.setIdentityNumber(tempUser.getIdentityNumber());
                    oUser.setUserEmail(tempUser.getUserEmail());
                    oUser.setUserEmail(formerUser.getUserEmail());
                    oUser.setUserId(tempUser.getId());
                    oUser.setUsername(tempUser.getUsername());
                    oUser.setUserRealName(tempUser.getUserRealName());
                    oUser.setFormerUserRealName(formerUser.getUserRealName());
                    oUser.setDeptId(tempUser.getDeptId());
                    oUser.setFormerDeptId(formerUser.getDeptId());
                    oUser.setSign(sign);
                    vo.setOldUser(oUser);
                    vo.setComAttachementFile(comAttachmentFile);
                    if(tempUser.getOldStatus()==null||tempUser.getOldStatus()==""){
                        tempUser.setOldStatus(StatusEnum.APPROVE.getValue());
                    }else {
                        tempUser.setOldStatus(StatusEnum.REEXAMINE.getValue());
                    }
                }
            }

            applyUserTempService.update(tempUser,Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getId,tempUser.getId()));
            Map<String, String> requestHeader = new HashMap<>();
            requestHeader.put("clientId", applyAdminConfig.getChannelClientId());
            requestHeader.put("clientSecret", applyAdminConfig.getChannelClientSecret());
            IResponse bb =applyAdminUseChannelFeign.addApplyUser(vo,requestHeader);
            if(!CommonConstants.SUCCESS.toString().equals(bb.getCode())){
                throw new AfsBaseException("申请失败!");
            }
        }else if(newCarRegion.size()>0&&oldCarRegion.size()>0){
            String sign=CommonConstants.STATUS_NORMAL;
            // 说明基础信息被修改，需要二边都发申请
            UserAuditVo vo=new UserAuditVo();
            List<SysUserRoleTemp> tempList =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()));

            List<SysUserRole> list =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,tempUser.getId()));
            if(tempList.size()>0&&list.size()>0){
                vo.setSysUserRoleTemp(tempList);
                vo.setSysUserRole(list);
            }else {
                throw new AfsBaseException("获取用户角色信息失败！");
            }
            // 声明一个值去判断是不是只有佣金和贷后
            String only=CommonConstants.STATUS_ERROR;
            for (SysUserRoleTemp temp:tempList){
                // 先声明默认
                if(!loan.getId().toString().equals(temp.getRoleId().toString())){
                    if(!brokerage.getId().toString().equals(temp.getRoleId().toString())){
                        only=CommonConstants.STATUS_NORMAL;
                    }
                }
            }
            if(newRole.size()>0&&oldRole.size()>0){
                //  说明新车二手车都有，那么需要判断是不是只有佣金贷后
                if(CommonConstants.STATUS_ERROR.equals(only)){
                    sign=CommonConstants.STATUS_ERROR;
                }
            }



            SysDepartment department =applyDepartmentMapper.selectById(tempUser.getDeptId());

            SysDepartment formorDepartment =applyDepartmentMapper.selectById(formerUser.getDeptId());
            if(newCarRegion.size()>0){
                if(compareNewCar==false){
                    if(newRole.size()>0){
                        //  说明我是新车  我要发起新车的审核
                        ApplyUserVo nUser =new ApplyUserVo();
                        nUser.setBusinessType(CarTypeEnum.newCar.toString());
                        nUser.setChannelId(tempUser.getChannelId());
                        if(department==null||formorDepartment==null){
                            throw new AfsBaseException("获取部门信息失败！");
                        }else {
                            nUser.setDepartmentName(department.getTitle());
                            nUser.setFormerDepartmentName(formorDepartment.getTitle());
                        }
                        nUser.setPhone(tempUser.getPhone());
                        nUser.setFormerPhone(formerUser.getPhone());
                        nUser.setIdentityNumber(tempUser.getIdentityNumber());
                        nUser.setUserEmail(tempUser.getUserEmail());
                        nUser.setFormerUserEmail(formerUser.getUserEmail());
                        nUser.setUserId(tempUser.getId());
                        nUser.setUsername(tempUser.getUsername());
                        nUser.setUserRealName(tempUser.getUserRealName());
                        nUser.setFormerUserRealName(formerUser.getUserRealName());
                        nUser.setDeptId(tempUser.getDeptId());
                        nUser.setFormerDeptId(formerUser.getDeptId());
                        nUser.setSign(sign);
                        vo.setNewUser(nUser);
                        vo.setComAttachementFile(comAttachmentFile);
                        if(tempUser.getNewStatus()==null||tempUser.getNewStatus()==""){
                            tempUser.setNewStatus(StatusEnum.APPROVE.getValue());
                        }else {
                            tempUser.setNewStatus(StatusEnum.REEXAMINE.getValue());
                        }

                    }else if(oldRole.size()>0){
                        // 说明我新车没了   我要发起二手车审核
                        ApplyUserVo oUser =new ApplyUserVo();
                        oUser.setBusinessType(CarTypeEnum.oldCar.toString());
                        oUser.setChannelId(tempUser.getChannelId());
                        if(department==null){
                            throw new AfsBaseException("获取部门信息失败！");
                        }else {
                            oUser.setDepartmentName(department.getTitle());
                            oUser.setFormerDepartmentName(formorDepartment.getTitle());
                        }
                        oUser.setPhone(tempUser.getPhone());
                        oUser.setFormerPhone(formerUser.getPhone());
                        oUser.setIdentityNumber(tempUser.getIdentityNumber());
                        oUser.setUserEmail(tempUser.getUserEmail());
                        oUser.setUserEmail(formerUser.getUserEmail());
                        oUser.setUserId(tempUser.getId());
                        oUser.setUsername(tempUser.getUsername());
                        oUser.setUserRealName(tempUser.getUserRealName());
                        oUser.setFormerUserRealName(formerUser.getUserRealName());
                        oUser.setDeptId(tempUser.getDeptId());
                        oUser.setFormerDeptId(formerUser.getDeptId());
                        oUser.setSign(sign);
                        vo.setOldUser(oUser);
                        vo.setComAttachementFile(comAttachmentFile);
                        if(tempUser.getOldStatus()==null||tempUser.getOldStatus()==""){
                            tempUser.setOldStatus(StatusEnum.APPROVE.getValue());
                        }else {
                            tempUser.setOldStatus(StatusEnum.REEXAMINE.getValue());
                        }
                    }
                }else if(oldRole.size()<=0){
                    //  说明我是新车没有改，但是二手车没了
                    ApplyUserVo nUser =new ApplyUserVo();
                    nUser.setBusinessType(CarTypeEnum.newCar.toString());
                    nUser.setChannelId(tempUser.getChannelId());
                    if(department==null||formorDepartment==null){
                        throw new AfsBaseException("获取部门信息失败！");
                    }else {
                        nUser.setDepartmentName(department.getTitle());
                        nUser.setFormerDepartmentName(formorDepartment.getTitle());
                    }
                    nUser.setPhone(tempUser.getPhone());
                    nUser.setFormerPhone(formerUser.getPhone());
                    nUser.setIdentityNumber(tempUser.getIdentityNumber());
                    nUser.setUserEmail(tempUser.getUserEmail());
                    nUser.setFormerUserEmail(formerUser.getUserEmail());
                    nUser.setUserId(tempUser.getId());
                    nUser.setUsername(tempUser.getUsername());
                    nUser.setUserRealName(tempUser.getUserRealName());
                    nUser.setFormerUserRealName(formerUser.getUserRealName());
                    nUser.setDeptId(tempUser.getDeptId());
                    nUser.setFormerDeptId(formerUser.getDeptId());
                    nUser.setSign(sign);
                    vo.setNewUser(nUser);
                    vo.setComAttachementFile(comAttachmentFile);
                    if(tempUser.getNewStatus()==null||tempUser.getNewStatus()==""){
                        tempUser.setNewStatus(StatusEnum.APPROVE.getValue());
                    }else {
                        tempUser.setNewStatus(StatusEnum.REEXAMINE.getValue());
                    }
                }
                // 再判断下上次是否被拒绝，如果拒绝那么就直接再次申请一遍
                if(newRole.size()>0&&StatusEnum.REFUSE.getValue().toString().equals(tempUser.getNewStatus())){
                    ApplyUserVo nUser =new ApplyUserVo();
                    nUser.setBusinessType(CarTypeEnum.newCar.toString());
                    nUser.setChannelId(tempUser.getChannelId());
                    if(department==null||formorDepartment==null){
                        throw new AfsBaseException("获取部门信息失败！");
                    }else {
                        nUser.setDepartmentName(department.getTitle());
                        nUser.setFormerDepartmentName(formorDepartment.getTitle());
                    }
                    nUser.setPhone(tempUser.getPhone());
                    nUser.setFormerPhone(formerUser.getPhone());
                    nUser.setIdentityNumber(tempUser.getIdentityNumber());
                    nUser.setUserEmail(tempUser.getUserEmail());
                    nUser.setFormerUserEmail(formerUser.getUserEmail());
                    nUser.setUserId(tempUser.getId());
                    nUser.setUsername(tempUser.getUsername());
                    nUser.setUserRealName(tempUser.getUserRealName());
                    nUser.setFormerUserRealName(formerUser.getUserRealName());
                    nUser.setDeptId(tempUser.getDeptId());
                    nUser.setFormerDeptId(formerUser.getDeptId());
                    nUser.setSign(sign);
                    vo.setNewUser(nUser);
                    vo.setComAttachementFile(comAttachmentFile);
                    if(tempUser.getNewStatus()==null||tempUser.getNewStatus()==""){
                        tempUser.setNewStatus(StatusEnum.APPROVE.getValue());
                    }else {
                        tempUser.setNewStatus(StatusEnum.REEXAMINE.getValue());
                    }
                }
            }
            if(oldCarRegion.size()>0){
                if(compareOldCar==false){
                    if(oldRole.size()>0){
                        // 说明我是二手车  我要发起二手车审核
                        ApplyUserVo oUser =new ApplyUserVo();
                        oUser.setBusinessType(CarTypeEnum.oldCar.toString());
                        oUser.setChannelId(tempUser.getChannelId());
                        if(department==null){
                            throw new AfsBaseException("获取部门信息失败！");
                        }else {
                            oUser.setDepartmentName(department.getTitle());
                            oUser.setFormerDepartmentName(formorDepartment.getTitle());
                        }
                        oUser.setPhone(tempUser.getPhone());
                        oUser.setFormerPhone(formerUser.getPhone());
                        oUser.setIdentityNumber(tempUser.getIdentityNumber());
                        oUser.setUserEmail(tempUser.getUserEmail());
                        oUser.setUserEmail(formerUser.getUserEmail());
                        oUser.setUserId(tempUser.getId());
                        oUser.setUsername(tempUser.getUsername());
                        oUser.setUserRealName(tempUser.getUserRealName());
                        oUser.setFormerUserRealName(formerUser.getUserRealName());
                        oUser.setDeptId(tempUser.getDeptId());
                        oUser.setFormerDeptId(formerUser.getDeptId());
                        oUser.setSign(sign);
                        vo.setOldUser(oUser);
                        vo.setComAttachementFile(comAttachmentFile);
                        if(tempUser.getOldStatus()==null||tempUser.getOldStatus()==""){
                            tempUser.setOldStatus(StatusEnum.APPROVE.getValue());
                        }else {
                            tempUser.setOldStatus(StatusEnum.REEXAMINE.getValue());
                        }
                    }else if(newRole.size()<=0){
                        // 说明我是二手车没动，但是新车没了
                        ApplyUserVo oUser =new ApplyUserVo();
                        oUser.setBusinessType(CarTypeEnum.oldCar.toString());
                        oUser.setChannelId(tempUser.getChannelId());
                        if(department==null){
                            throw new AfsBaseException("获取部门信息失败！");
                        }else {
                            oUser.setDepartmentName(department.getTitle());
                            oUser.setFormerDepartmentName(formorDepartment.getTitle());
                        }
                        oUser.setPhone(tempUser.getPhone());
                        oUser.setFormerPhone(formerUser.getPhone());
                        oUser.setIdentityNumber(tempUser.getIdentityNumber());
                        oUser.setUserEmail(tempUser.getUserEmail());
                        oUser.setUserEmail(formerUser.getUserEmail());
                        oUser.setUserId(tempUser.getId());
                        oUser.setUsername(tempUser.getUsername());
                        oUser.setUserRealName(tempUser.getUserRealName());
                        oUser.setFormerUserRealName(formerUser.getUserRealName());
                        oUser.setDeptId(tempUser.getDeptId());
                        oUser.setFormerDeptId(formerUser.getDeptId());
                        oUser.setSign(sign);
                        vo.setOldUser(oUser);
                        vo.setComAttachementFile(comAttachmentFile);
                        if(tempUser.getOldStatus()==null||tempUser.getOldStatus()==""){
                            tempUser.setOldStatus(StatusEnum.APPROVE.getValue());
                        }else {
                            tempUser.setOldStatus(StatusEnum.REEXAMINE.getValue());
                        }
                    }
                }else if(newRole.size()<=0){
                    // 说明我是二手车没动，但是新车没了
                    ApplyUserVo oUser =new ApplyUserVo();
                    oUser.setBusinessType(CarTypeEnum.oldCar.toString());
                    oUser.setChannelId(tempUser.getChannelId());
                    if(department==null){
                        throw new AfsBaseException("获取部门信息失败！");
                    }else {
                        oUser.setDepartmentName(department.getTitle());
                        oUser.setFormerDepartmentName(formorDepartment.getTitle());
                    }
                    oUser.setPhone(tempUser.getPhone());
                    oUser.setFormerPhone(formerUser.getPhone());
                    oUser.setIdentityNumber(tempUser.getIdentityNumber());
                    oUser.setUserEmail(tempUser.getUserEmail());
                    oUser.setUserEmail(formerUser.getUserEmail());
                    oUser.setUserId(tempUser.getId());
                    oUser.setUsername(tempUser.getUsername());
                    oUser.setUserRealName(tempUser.getUserRealName());
                    oUser.setFormerUserRealName(formerUser.getUserRealName());
                    oUser.setDeptId(tempUser.getDeptId());
                    oUser.setFormerDeptId(formerUser.getDeptId());
                    oUser.setSign(sign);
                    vo.setOldUser(oUser);
                    vo.setComAttachementFile(comAttachmentFile);
                    if(tempUser.getOldStatus()==null||tempUser.getOldStatus()==""){
                        tempUser.setOldStatus(StatusEnum.APPROVE.getValue());
                    }else {
                        tempUser.setOldStatus(StatusEnum.REEXAMINE.getValue());
                    }
                }
                // 判断二手车上次是不是被拒绝的
                if(oldRole.size()>0&&StatusEnum.REFUSE.getValue().toString().equals(tempUser.getOldStatus())){
                    ApplyUserVo oUser =new ApplyUserVo();
                    oUser.setBusinessType(CarTypeEnum.oldCar.toString());
                    oUser.setChannelId(tempUser.getChannelId());
                    if(department==null){
                        throw new AfsBaseException("获取部门信息失败！");
                    }else {
                        oUser.setDepartmentName(department.getTitle());
                        oUser.setFormerDepartmentName(formorDepartment.getTitle());
                    }
                    oUser.setPhone(tempUser.getPhone());
                    oUser.setFormerPhone(formerUser.getPhone());
                    oUser.setIdentityNumber(tempUser.getIdentityNumber());
                    oUser.setUserEmail(tempUser.getUserEmail());
                    oUser.setUserEmail(formerUser.getUserEmail());
                    oUser.setUserId(tempUser.getId());
                    oUser.setUsername(tempUser.getUsername());
                    oUser.setUserRealName(tempUser.getUserRealName());
                    oUser.setFormerUserRealName(formerUser.getUserRealName());
                    oUser.setDeptId(tempUser.getDeptId());
                    oUser.setFormerDeptId(formerUser.getDeptId());
                    oUser.setSign(sign);
                    vo.setOldUser(oUser);
                    vo.setComAttachementFile(comAttachmentFile);
                    if(tempUser.getOldStatus()==null||tempUser.getOldStatus()==""){
                        tempUser.setOldStatus(StatusEnum.APPROVE.getValue());
                    }else {
                        tempUser.setOldStatus(StatusEnum.REEXAMINE.getValue());
                    }
                }
            }

            if(vo.getNewUser()==null&&vo.getOldUser()==null){
                throw new AfsBaseException("暂无数据修改！");
            }
            applyUserTempService.update(tempUser,Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getId,tempUser.getId()));
            Map<String, String> requestHeader = new HashMap<>();
            requestHeader.put("clientId", applyAdminConfig.getChannelClientId());
            requestHeader.put("clientSecret", applyAdminConfig.getChannelClientSecret());
            IResponse bb =applyAdminUseChannelFeign.addApplyUser(vo,requestHeader);
            if(!CommonConstants.SUCCESS.toString().equals(bb.getCode())){
                throw new AfsBaseException("申请失败!");
            }
        } else  {
            if(newCarRegion.size()>0&&oldCarRegion.size()<=0){
                if(compareNewCar){
                    if(StatusEnum.REFUSE.getValue().equals(tempUser.getNewStatus())){

                    }else {
                        throw new AfsBaseException("新车角色暂无变化！");
                    }
                }
                // 说明只改了新车
                String sign=CommonConstants.STATUS_NORMAL;
                // 说明基础信息被修改，需要二边都发申请
                UserAuditVo vo=new UserAuditVo();
                List<SysUserRoleTemp> tempList =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()));
                List<SysUserRole> list =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,tempUser.getId()));
                if(tempList.size()>0&&list.size()>0){
                    vo.setSysUserRoleTemp(tempList);
                    vo.setSysUserRole(list);
                }else {
                    throw new AfsBaseException("获取用户角色信息失败！");
                }
                // 声明一个值去判断是不是只有佣金和贷后
                String only=CommonConstants.STATUS_ERROR;
                for (SysUserRoleTemp temp:tempList){
                    // 先声明默认
                    if(!loan.getId().toString().equals(temp.getRoleId().toString())){
                        if(!brokerage.getId().toString().equals(temp.getRoleId().toString())){
                            only=CommonConstants.STATUS_NORMAL;
                        }
                    }
                }
                if(newRole.size()>0&&oldRole.size()>0){
                    //  说明新车二手车都有，那么需要判断是不是只有佣金贷后
                    if(CommonConstants.STATUS_ERROR.equals(only)){
                        sign=CommonConstants.STATUS_ERROR;
                    }
                }



                SysDepartment department =applyDepartmentMapper.selectById(tempUser.getDeptId());

                SysDepartment formorDepartment =applyDepartmentMapper.selectById(formerUser.getDeptId());
                if(newRole.size()>0){
                    //  说明我是新车  我要发起新车的审核
                    ApplyUserVo nUser =new ApplyUserVo();
                    nUser.setBusinessType(CarTypeEnum.newCar.toString());
                    nUser.setChannelId(tempUser.getChannelId());
                    if(department==null||formorDepartment==null){
                        throw new AfsBaseException("获取部门信息失败！");
                    }else {
                        nUser.setDepartmentName(department.getTitle());
                        nUser.setFormerDepartmentName(formorDepartment.getTitle());
                    }
                    nUser.setPhone(tempUser.getPhone());
                    nUser.setFormerPhone(formerUser.getPhone());
                    nUser.setIdentityNumber(tempUser.getIdentityNumber());
                    nUser.setUserEmail(tempUser.getUserEmail());
                    nUser.setFormerUserEmail(formerUser.getUserEmail());
                    nUser.setUserId(tempUser.getId());
                    nUser.setUsername(tempUser.getUsername());
                    nUser.setUserRealName(tempUser.getUserRealName());
                    nUser.setFormerUserRealName(formerUser.getUserRealName());
                    nUser.setDeptId(tempUser.getDeptId());
                    nUser.setFormerDeptId(formerUser.getDeptId());
                    nUser.setSign(sign);
                    vo.setNewUser(nUser);
                    vo.setComAttachementFile(comAttachmentFile);
                    if(tempUser.getNewStatus()==null||tempUser.getNewStatus()==""){
                        tempUser.setNewStatus(StatusEnum.APPROVE.getValue());
                    }else {
                        tempUser.setNewStatus(StatusEnum.REEXAMINE.getValue());
                    }
                }
                applyUserTempService.update(tempUser,Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getId,tempUser.getId()));
                Map<String, String> requestHeader = new HashMap<>();
                requestHeader.put("clientId", applyAdminConfig.getChannelClientId());
                requestHeader.put("clientSecret", applyAdminConfig.getChannelClientSecret());
                IResponse bb =applyAdminUseChannelFeign.addApplyUser(vo,requestHeader);
                if(!CommonConstants.SUCCESS.toString().equals(bb.getCode())){
                    throw new AfsBaseException("申请失败!");
                }
            }else if(oldCarRegion.size()>0&&newCarRegion.size()<=0) {
                if(compareOldCar){
                    if (StatusEnum.REFUSE.getValue().toString().equals(tempUser.getOldStatus())){

                    }else {
                        throw new AfsBaseException("二手车角色暂无变化！");
                    }
                }
                // 说明只改了二手车
                String sign=CommonConstants.STATUS_NORMAL;
                // 说明基础信息被修改，需要二边都发申请
                UserAuditVo vo=new UserAuditVo();
                List<SysUserRoleTemp> tempList =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()));

                List<SysUserRole> list =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,tempUser.getId()));
                if(tempList.size()>0&&list.size()>0){
                    vo.setSysUserRoleTemp(tempList);
                    vo.setSysUserRole(list);
                }else {
                    throw new AfsBaseException("获取用户角色信息失败！");
                }
                // 声明一个值去判断是不是只有佣金和贷后
                String only=CommonConstants.STATUS_ERROR;
                for (SysUserRoleTemp temp:tempList){
                    // 先声明默认
                    if(!loan.getId().toString().equals(temp.getRoleId().toString())){
                        if(!brokerage.getId().toString().equals(temp.getRoleId().toString())){
                            only=CommonConstants.STATUS_NORMAL;
                        }
                    }
                }
                if(newRole.size()>0&&oldRole.size()>0){
                    //  说明新车二手车都有，那么需要判断是不是只有佣金贷后
                    if(CommonConstants.STATUS_ERROR.equals(only)){
                        sign=CommonConstants.STATUS_ERROR;
                    }
                }



                SysDepartment department =applyDepartmentMapper.selectById(tempUser.getDeptId());

                SysDepartment formorDepartment =applyDepartmentMapper.selectById(formerUser.getDeptId());
                if(oldRole.size()>0){
                    // 说明我是二手车  我要发起二手车审核
                    ApplyUserVo oUser =new ApplyUserVo();
                    oUser.setBusinessType(CarTypeEnum.oldCar.toString());
                    oUser.setChannelId(tempUser.getChannelId());
                    if(department==null){
                        throw new AfsBaseException("获取部门信息失败！");
                    }else {
                        oUser.setDepartmentName(department.getTitle());
                    }
                    oUser.setPhone(tempUser.getPhone());
                    oUser.setFormerPhone(formerUser.getPhone());
                    oUser.setIdentityNumber(tempUser.getIdentityNumber());
                    oUser.setUserEmail(tempUser.getUserEmail());
                    oUser.setUserEmail(formerUser.getUserEmail());
                    oUser.setUserId(tempUser.getId());
                    oUser.setUsername(tempUser.getUsername());
                    oUser.setUserRealName(tempUser.getUserRealName());
                    oUser.setFormerUserRealName(formerUser.getUserRealName());
                    oUser.setDeptId(tempUser.getDeptId());
                    oUser.setFormerDeptId(formerUser.getDeptId());
                    oUser.setSign(sign);
                    vo.setOldUser(oUser);
                    vo.setComAttachementFile(comAttachmentFile);
                    if(tempUser.getOldStatus()==null||tempUser.getOldStatus()==""){
                        tempUser.setOldStatus(StatusEnum.APPROVE.getValue());
                    }else {
                        tempUser.setOldStatus(StatusEnum.REEXAMINE.getValue());
                    }
                }

                applyUserTempService.update(tempUser,Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getId,tempUser.getId()));
                Map<String, String> requestHeader = new HashMap<>();
                requestHeader.put("clientId", applyAdminConfig.getChannelClientId());
                requestHeader.put("clientSecret", applyAdminConfig.getChannelClientSecret());
                IResponse bb =applyAdminUseChannelFeign.addApplyUser(vo,requestHeader);
                if(!CommonConstants.SUCCESS.toString().equals(bb.getCode())){
                    throw new AfsBaseException("申请失败!");
                }
            }else {
                throw new AfsBaseException("暂无修改内容！");
            }

        }








        return IResponse.success(Boolean.TRUE).setMsg("操作成功！");
    }


    @Override
    public void chackRole(ApplyUserCondition userCondition) {
        this.check(userCondition);
    }

    @Override
    public void enableUser(Long id) {
        SysUser oldUser = this.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUserId, id));

        if (oldUser == null) {
            throw new AfsBaseException(CommonConstants.FAIL, "通过userId获取用户失败");
        }
        List<SysUser> userList = this.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getIdentityNumber, oldUser.getIdentityNumber()));
        if (userList.size() > 0) {
            for (SysUser user : userList) {
                if (DataAccessUtil.getChannelId().longValue() != user.getChannelId().longValue() && oldUser.getIdentityNumber().equals(user.getIdentityNumber())) {
                    if (StatusEnum.NO.getValue().equals(user.getLockFlag())) {
                        throw new AfsBaseException(CommonConstants.FAIL, "系统已在其他渠道存在此身份证号用户，且为启用状态，该用户不能启用！");
                    }
                }
            }
        }

        List<SysUserRole> newCarUserRoles = userRoleService.list(Wrappers.<SysUserRole>query().lambda()
                .eq(SysUserRole::getUserId, id)
                .eq(SysUserRole::getBusinessType, CarTypeEnum.newCar));
        List<SysUserRole> oldCarUserRoles = userRoleService.list(Wrappers.<SysUserRole>query().lambda()
                .eq(SysUserRole::getUserId, id)
                .eq(SysUserRole::getBusinessType, CarTypeEnum.oldCar));
        chackUserCount(newCarUserRoles.size(), oldCarUserRoles.size());

        oldUser.setLockFlag(StatusEnum.NO.getValue());
        this.updateById(oldUser);
    }

    /**
     * 用户新增相关校验
     *
     * @param userCondition
     * @return
     */
    public IResponse<Boolean> check(ApplyUserCondition userCondition) {

        // 获取这个部门能做新车还是二手车  还是都可以
        List<SysDeptAuthRegion> newCarRegion = applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda()
                .eq(DataAccessUtil.isAdmin(), SysDeptAuthRegion::getChannelId, userCondition.getChannelId())
                .eq(SysDeptAuthRegion::getDeptId, userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType, CarTypeEnum.newCar));

        List<SysDeptAuthRegion> oldCarRegion = applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda()
                .eq(DataAccessUtil.isAdmin(), SysDeptAuthRegion::getChannelId, userCondition.getChannelId())
                .eq(SysDeptAuthRegion::getDeptId, userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType, CarTypeEnum.oldCar));

        if (StrUtil.isBlank(userCondition.getUsername())) {
            throw new AfsBaseException(CommonConstants.FAIL, "用户名为空");
        }
        //  我第一步应该是拿到这个集合 然后 把 数据进行分类。分别为 ：新车  二手车  全有
        List<Long> newRoleIds=new ArrayList<>();
        List<Long> oldRoleIds =new ArrayList<>();
        List<Long> list = new ArrayList<>();

        if(userCondition.getNewRoleIds()!=null){
            newRoleIds= Arrays.asList(userCondition.getNewRoleIds());
            list.addAll(newRoleIds);
        }
        if(userCondition.getOldRoleIds()!=null){
            oldRoleIds= Arrays.asList(userCondition.getOldRoleIds());
            list.addAll(oldRoleIds);
        }


        // 查询所有我添加的角色信息
        List<SysRole> roleList = applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getId, list));
        // 放所有的我选择的角色的code 码
        List<String> roleAllList = new ArrayList<>();
        // 存放新车的code码
        List<String> newCarRoleList = new ArrayList<>();
        //存放二手车的code码
        List<String> oldCarRoleList = new ArrayList<>();
        // 这个应该是新车和二手车都有的code码
        List<String> carRoleList = new ArrayList<>();
        for (SysRole role : roleList) {
            roleAllList.add(role.getRoleCode());
            if (CarTypeEnum.newCar.name().equals(role.getRoleType())) {
                newCarRoleList.add(role.getRoleCode());
            } else if (CarTypeEnum.oldCar.name().equals(role.getRoleType())) {
                oldCarRoleList.add(role.getRoleCode());
            } else {
                carRoleList.add(role.getRoleCode());
            }
        }
        // 新版校验用户数量
        chackSaveUserCount(newCarRoleList, oldCarRoleList, carRoleList, newCarRegion, oldCarRegion,userCondition.getUserId(),userCondition.getChannelId());
        // 如果是添加 进去 修改不进去
        if (userCondition.getUserId() == null) {
            SysUser applyUser = this.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, userCondition.getUsername()));
            if (applyUser != null) {
                if (StatusEnum.NO.getValue().equals(applyUser.getLockFlag())) {
                    throw new AfsBaseException(CommonConstants.FAIL, "系统已存在此用户名用户，请不要重复创建！");
                } else if (StatusEnum.YES.getValue().equals(applyUser.getLockFlag())) {
                    throw new AfsBaseException(CommonConstants.FAIL, "系统已存在此用户名用户，为停用状态，请不要重复创建，请将原账号启用！");
                }
            }
        }
        //  这个地方去校验 一下新车预防款员必须带一个  新车预录单员 或者新车申请报单员
        if(roleAllList.contains(RoleEnum.ROLE_XCPREPAYMENT.name())){
            // 说明是新车放款那么举要看看他有没有另外
            if(!roleAllList.contains(RoleEnum.ROLE_XCRECORDED.name())){
                // 说明没有新车预录单员
                if(!roleAllList.contains(RoleEnum.ROLE_XCAPPLY.name())){
                    throw new AfsBaseException("新车预防款员必须拥有新车预录单员或者新车申请报单员！");
                }
            }
        }
        if(roleAllList.contains(RoleEnum.ROLE_PREPAYMENT.name())){
            // 说明是二手车放款员，那么要看看他有没有其他两个
            if(!roleAllList.contains(RoleEnum.ROLE_RECORDED.name())){
                if(!roleAllList.contains(RoleEnum.ROLE_APPLY.name())){
                    throw new AfsBaseException("二手车预防款员必须拥有二手车预录单员或者二手车申请报单员！");
                }
            }
        }

        if (roleAllList.contains(RoleEnum.ROLE_RECORDED.name()) && roleAllList.contains(RoleEnum.ROLE_APPLY.name())) {
            throw new AfsBaseException(CommonConstants.FAIL, "当前选择的角色预录单员角色与申请报单员角色不相容！");
        }
        if (roleAllList.contains(RoleEnum.ROLE_PAYMENT.name()) && roleAllList.contains(RoleEnum.ROLE_PREPAYMENT.name())) {
            throw new AfsBaseException(CommonConstants.FAIL, "当前选择的角色预放款角色与放款角色不相容！");
        }
        if (roleAllList.contains(RoleEnum.ROLE_XCRECORDED.name()) && roleAllList.contains(RoleEnum.ROLE_XCAPPLY.name())) {
            throw new AfsBaseException(CommonConstants.FAIL, "当前选择的角色新车预录单员角色与新车申请报单员角色不相容！");
        }
        if (roleAllList.contains(RoleEnum.ROLE_XCPAYMENT.name()) && roleAllList.contains(RoleEnum.ROLE_XCPREPAYMENT.name())) {
            throw new AfsBaseException(CommonConstants.FAIL, "当前选择的角色新车预放款角色与新车放款角色不相容！");
        }
        if (userCondition.getUserId() == null) {
            List<SysUser> chanUser = this.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getIdentityNumber, userCondition.getIdentityNumber()).eq(SysUser::getChannelId, userCondition.getChannelId()));
            // 最多一个身份证在这个渠道有1次，如果有2次  说明这个这个身份证号搜索渠道管理员自己
            if (chanUser.size() <= 2) {
                for (SysUser cuser : chanUser) {
                    if (StatusEnum.YES.getValue().equals(cuser.getLockFlag())) {
                        throw new AfsBaseException(CommonConstants.FAIL, "系统已存在相同身份证，为停用状态，请不要重复创建，请将原账号启用！");
                    }
                }
                if(chanUser.size()==2){
                    throw new AfsBaseException(CommonConstants.FAIL, "系统已存在相同身份证，请不要重复创建！");
                }
            } else {
                throw new AfsBaseException(CommonConstants.FAIL, "系统已存在相同身份证，请不要重复创建！");
            }
            //  当等于1的时候  要不是管理员要不就直接结束提示用户已存在
            if (chanUser.size() == 1) {
                // 判断这个身份证号是管理员的身份证号吗
                SysChannelBasicInfo basicInfo = applyChannelBasicInfoService.getOne(Wrappers.<SysChannelBasicInfo>query().lambda().eq(SysChannelBasicInfo::getChannelId, userCondition.getChannelId()));
                if (userCondition.getIdentityNumber().equals(basicInfo.getIdentityNumber())) {
                    // 说明这个人是管理员可以再创建一个

                } else {
                    // 说明不是管理员不可以创建了
                    if (StatusEnum.YES.getValue().equals(chanUser.get(0).getLockFlag())) {
                        throw new AfsBaseException(CommonConstants.FAIL, "系统已存在相同身份证，为停用状态，请不要重复创建，请将原账号启用！");
                    } else {
                        throw new AfsBaseException(CommonConstants.FAIL, "系统已存在相同身份证，请不要重复创建！");
                    }
                }
            }
        }

        // 再拿这个身份证号去判断在其它渠道有没有被启用的状态
        List<SysUser> userList = this.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getIdentityNumber, userCondition.getIdentityNumber()).eq(SysUser::getLockFlag, StatusEnum.NO.getValue().toString()).eq(SysUser::getDelFlag, "0").notIn(SysUser::getChannelId, userCondition.getChannelId().toString()));
        if (userList.size() > 0) {
            throw new AfsBaseException(CommonConstants.FAIL, "此身份证号已在其他渠道启用！");
        }


        // 下面是去校验见证人是不是见证人
        this.checkUserCard(userCondition.getIdentityNumber(), newCarRoleList, oldCarRoleList,userCondition.getChannelId());
        //  下面 注释的都是以前使用的校验 暂时注释。以后删除
//        SysChannelBasicInfo channelBasicInfo = channelBasicInfoService.getOne(Wrappers.<SysChannelBasicInfo>query().lambda().
//                eq(SysChannelBasicInfo::getChannelId, DataAccessUtil.getChannelId()));
//
//        //如果渠道是SP
//        if (ChannelTypeEnum.SP.equals(channelBasicInfo.getChannelType())) {
//            //获取该身份证号的见证人
//            List<SysChannelWitnessInfo> channelWitnessInfoList = channelWitnessInfoService.list(Wrappers.<SysChannelWitnessInfo>query().lambda().
//                    eq(SysChannelWitnessInfo::getWitnessIdCard, userCondition.getIdentityNumber()).eq(SysChannelWitnessInfo::getStatus, LockFlagEnum.YES));
//            checkNewCarOrOldCar(channelWitnessInfoList, newCarRoleList, oldCarRoleList, carRoleList);
//            //如果渠道是直营
//        } else if (ChannelTypeEnum.direct.equals(channelBasicInfo.getChannelType())) {
//            //获取所有渠道是SP下的该身份证号的见证人
//            List<SysChannelWitnessInfo> channelWitnessInfoList = this.getWitnessList(ChannelTypeEnum.SP.name(), userCondition.getIdentityNumber());
//            checkNewCarOrOldCar(channelWitnessInfoList, newCarRoleList, oldCarRoleList, carRoleList);
//        }
        return IResponse.success(Boolean.TRUE);
    }

    // 重写见证人的校验
    public void checkUserCard(String identityNumber, List<String> newCarRoleList, List<String> oldCarRoleList,Long channelId) {
        // 先判断这个人在其他渠道是不是存在可以用的见证人状态
        List<SysChannelWitnessInfo> witnessInfos = channelWitnessInfoService.list(Wrappers.<SysChannelWitnessInfo>query().lambda().
                eq(SysChannelWitnessInfo::getWitnessIdCard, identityNumber).notIn(SysChannelWitnessInfo::getChannelId, channelId).eq(SysChannelWitnessInfo::getStatus, LockFlagEnum.YES));
        if (witnessInfos.size() > 0) {
            throw new AfsBaseException(CommonConstants.FAIL, "该身份证号已启用其他合作商！");
        }
        // 排除新车里面的新车的预录单员和申报单员
        Set<String> set = new HashSet<String>();
        for (String str : newCarRoleList) {
            if (RoleEnum.ROLE_XCAPPLY.name().toString().equals(str.toString())) {

            } else if (RoleEnum.ROLE_XCRECORDED.name().toString().equals(str.toString())) {

            } else {
                set.add(str);
            }

        }
        if (set.size() > 0) {
            List<SysChannelWitnessInfo> witnessList = channelWitnessInfoService.list(Wrappers.<SysChannelWitnessInfo>query().lambda().
                    eq(SysChannelWitnessInfo::getWitnessIdCard, identityNumber).eq(SysChannelWitnessInfo::getStatus, LockFlagEnum.YES).eq(SysChannelWitnessInfo::getChannelId, channelId).eq(SysChannelWitnessInfo::getBusinessAttributes, CarTypeEnum.newCar));
            if (witnessList.size() > 0) {

            } else {
                throw new AfsBaseException(CommonConstants.FAIL, "该身份证号不是新车见证人！");
            }
        }
        if (oldCarRoleList.size() > 0) {
            List<SysChannelWitnessInfo> witnessList = channelWitnessInfoService.list(Wrappers.<SysChannelWitnessInfo>query().lambda().
                    eq(SysChannelWitnessInfo::getWitnessIdCard, identityNumber).eq(SysChannelWitnessInfo::getStatus, LockFlagEnum.YES).eq(SysChannelWitnessInfo::getChannelId,channelId).eq(SysChannelWitnessInfo::getBusinessAttributes, CarTypeEnum.oldCar));
            if (witnessList.size() > 0) {

            } else {
                throw new AfsBaseException(CommonConstants.FAIL, "该身份证号不是二手车见证人！");
            }
        }

    }

    public void checkNewCarOrOldCar(List<SysChannelWitnessInfo> channelWitnessInfoList, List<String> newCarRoleList,
                                    List<String> oldCarRoleList, List<String> carRoleList) {
        //如果角色是新车组
        if (newCarRoleList.size() > 0) {
            //如果角色里有预放款角色和放款角色的其中一个
            if ((newCarRoleList.contains(RoleEnum.ROLE_XCPAYMENT.name()) || newCarRoleList.contains(RoleEnum.ROLE_XCPREPAYMENT.name()))) {
                if (channelWitnessInfoList.size() == 1) {
                    //判断是否是本渠道或是否是本业务见证人
                    if (!CarTypeEnum.newCar.name().equals(channelWitnessInfoList.get(0).getBusinessAttributes())
                            || !DataAccessUtil.getChannelId().equals(channelWitnessInfoList.get(0).getChannelId())) {
                        throw new AfsBaseException(CommonConstants.FAIL, "非本渠道或非新车组的见证人，请重新创建！");
                    }
                } else if (channelWitnessInfoList.size() == 0) {
                    throw new AfsBaseException(CommonConstants.FAIL, "该身份证号不是见证人，或见证人处于失效状态不可用，请重新创建！");
                }
            }
            //如果角色是二手车组
        } else if (oldCarRoleList.size() > 0) {
            //如果角色里包含除佣金、贷后两角色中任一个
            if (!oldCarRoleList.contains(RoleEnum.ROLE_COMMISSION.name()) && !oldCarRoleList.contains(RoleEnum.ROLE_LOANAFTER.name())) {
                if (channelWitnessInfoList.size() == 1) {
                    //判断是否是本渠道或是否是本业务见证人
                    if (!CarTypeEnum.oldCar.name().equals(channelWitnessInfoList.get(0).getBusinessAttributes())
                            || !DataAccessUtil.getChannelId().equals(channelWitnessInfoList.get(0).getChannelId())) {
                        throw new AfsBaseException(CommonConstants.FAIL, "非本渠道或非二手车组的见证人，请重新创建！");
                    }
                } else if (channelWitnessInfoList.size() == 0) {
                    throw new AfsBaseException(CommonConstants.FAIL, "该身份证号不是见证人，或见证人处于失效状态不可用，请重新创建！");
                }
            }
        } else {
            //如果一个角色是新车二手车组
            if (!ObjectUtils.isEmpty(newCarRoleList) && !ObjectUtils.isEmpty(oldCarRoleList)) {
                //如果角色是新车组、如果角色里有预放款角色和放款角色的其中一个
                //如果角色是二手车组、如果角色里包含除佣金、贷后两角色中任一个
                if ((!oldCarRoleList.contains(RoleEnum.ROLE_COMMISSION.name()) && !oldCarRoleList.contains(RoleEnum.ROLE_LOANAFTER.name()))
                        || (newCarRoleList.contains(RoleEnum.ROLE_XCPAYMENT.name()) || newCarRoleList.contains(RoleEnum.ROLE_XCPREPAYMENT.name()))) {
                    if (channelWitnessInfoList.size() == 1) {
                        //判断是否是本渠道或是否是本业务见证人
                        if (!DataAccessUtil.getChannelId().equals(channelWitnessInfoList.get(0).getChannelId())) {
                            throw new AfsBaseException(CommonConstants.FAIL, "非本渠道或非新车二手车组合组的见证人，请重新创建！");
                        }
                    } else if (channelWitnessInfoList.size() == 0) {
                        throw new AfsBaseException(CommonConstants.FAIL, "该身份证号不是见证人，或见证人处于失效状态不可用，请重新创建！");
                    }
                }
                //如果多个角色里面有新车又有二手车
                //如果角色是新车组、如果角色里有预放款角色和放款角色的其中一个
                //如果角色是二手车组、如果角色里包含除佣金、贷后两角色中任一个
            } else if ((!carRoleList.contains(RoleEnum.ROLE_COMMISSION.name()) && !carRoleList.contains(RoleEnum.ROLE_LOANAFTER.name()))
                    || (carRoleList.contains(RoleEnum.ROLE_XCPAYMENT.name()) || carRoleList.contains(RoleEnum.ROLE_XCPREPAYMENT.name()))) {
                if (channelWitnessInfoList.size() == 1) {
                    //判断是否是本渠道或是否是本业务见证人
                    if (!DataAccessUtil.getChannelId().equals(channelWitnessInfoList.get(0).getChannelId())) {
                        throw new AfsBaseException(CommonConstants.FAIL, "非本渠道或非新车二手车组合组的见证人，请重新创建！");
                    }
                } else if (channelWitnessInfoList.size() == 0) {
                    throw new AfsBaseException(CommonConstants.FAIL, "该身份证号不是见证人，或见证人处于失效状态不可用，请重新创建！");
                }
            }
        }
    }

    /**
     * 校验新车二手车用户数量
     */
    private void chackUserCount(Integer newRoleIds, Integer oldRoleIds) {

        SysChannelBasicInfo one = applyChannelBasicInfoService.getOne(Wrappers.<SysChannelBasicInfo>lambdaQuery().eq(SysChannelBasicInfo::getChannelId, DataAccessUtil.getChannelId()));
        // 先去查下下管理员的id
        List<SysRole> roles = applyRoleService.list(Wrappers.<SysRole>lambdaQuery().eq(SysRole::getRoleCode, RoleEnum.ROLE_APPLY.name()));
        // 声明一个用户管理员账号的角色集合
        List<SysUserRole> list = new ArrayList<>();
        if (roles.size() > 0) {
            // 先去看看管理员觉得在 sys_user_role 表里有几条数据
            list = userRoleService.list(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getRoleId, roles.get(0).getId()));
        }
        if (newRoleIds > 0) {
            Integer newCarUserHierarchy = one.getNewCarUserHierarchy();
            if (newCarUserHierarchy == null) {
                throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置新车账号最大数量，请联系业务人员处理");
            }
            Integer newCarUserCount = baseMapper.findUserCount(CarTypeEnum.newCar.name(), DataAccessUtil.getChannelId());
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    if (CarTypeEnum.newCar.name().equals(list.get(i).getBusinessType())) {
                        // 这边加1 是为了排除自己的账号
                        newCarUserHierarchy = newCarUserHierarchy + 1;
                    }
                }
            }

            if (newCarUserCount > newCarUserHierarchy) {
                throw new AfsBaseException(CommonConstants.FAIL, "新车用户数量超过最大限制");
            }
        }
        if (oldRoleIds > 0) {
            Integer oldCarUserHierarchy = one.getOldCarUserHierarchy();
            if (oldCarUserHierarchy == null) {
                throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置二手车账号最大数量，请联系业务人员处理");
            }
            Integer oldCarUserCount = baseMapper.findUserCount(CarTypeEnum.oldCar.name(), DataAccessUtil.getChannelId());
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    if (CarTypeEnum.oldCar.name().equals(list.get(i).getBusinessType())) {
                        // 这边加1 是为了排除自己的账号
                        oldCarUserHierarchy = oldCarUserHierarchy + 1;
                    }
                }
            }
            if (oldCarUserCount > oldCarUserHierarchy) {
                throw new AfsBaseException(CommonConstants.FAIL, "二手车用户数量超过最大限制");
            }
        }
    }

    private void chackSaveUserCount(List<String> newCarRoleList, List<String> oldCarRoleList, List<String> carRoleList, List<SysDeptAuthRegion> newCar, List<SysDeptAuthRegion> oldCar,Long userId,Long channelId) {
        // 先查询 这个账户新车可以创建多少
        SysChannelBasicInfo one = applyChannelBasicInfoService.getOne(Wrappers.<SysChannelBasicInfo>lambdaQuery().eq(SysChannelBasicInfo::getChannelId, channelId));
        if (one != null) {
            // 声明一个新车 的用户数量
            Integer newCount = one.getNewCarUserHierarchy();
            Integer oldCount = one.getOldCarUserHierarchy();
            Integer newCarUserCount =0;
            Integer oldCarUserCount=0;
            if(userId==null){
                newCarUserCount = baseMapper.findUserCount(CarTypeEnum.newCar.name(), channelId);
                oldCarUserCount = baseMapper.findUserCount(CarTypeEnum.oldCar.name(), channelId);
            }else {
                List<Long> uId=new ArrayList<>();
                uId.add(userId);
                newCarUserCount = baseMapper.findUserCountNotInUser(CarTypeEnum.newCar.name(), channelId,uId);
                oldCarUserCount = baseMapper.findUserCountNotInUser(CarTypeEnum.oldCar.name(), channelId,uId);
            }


            // 查询这个渠道
            if (newCarRoleList.size() > 0) {
                if (newCount == null) {
                    throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置新车账号最大数量，请联系业务人员处理");
                }
                // 需要去判断新车的用户数量

                if (newCarUserCount >= newCount + 1) {
                    throw new AfsBaseException(CommonConstants.FAIL, "新车用户数量超过最大限制");
                }

            }
            if (oldCarRoleList.size() > 0) {
                if (oldCount == null) {
                    throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置二手车账号最大数量，请联系业务人员处理");
                }

                if (oldCarUserCount >= oldCount + 1) {
                    throw new AfsBaseException(CommonConstants.FAIL, "二手车用户数量超过最大限制");
                }
            }
            if (carRoleList.size() > 0) {
                // 这个就要看传递过来的区域是只有新车  还是只有二手车  还是都有
                if (newCar.size() > 0 && oldCar.size() > 0) {
                    if (newCount == null) {
                        throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置新车账号最大数量，请联系业务人员处理");
                    }
                    if (oldCount == null) {
                        throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置二手车账号最大数量，请联系业务人员处理");
                    }
                    if (newCarUserCount >= newCount + 1) {
                        throw new AfsBaseException(CommonConstants.FAIL, "新车用户数量超过最大限制");
                    }
                    if (oldCarUserCount >= oldCount + 1) {
                        throw new AfsBaseException(CommonConstants.FAIL, "二手车用户数量超过最大限制");
                    }
                } else if (newCar.size() > 0) {
                    if (newCount == null) {
                        throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置新车账号最大数量，请联系业务人员处理");
                    }
                    if (newCarUserCount >= newCount + 1) {
                        throw new AfsBaseException(CommonConstants.FAIL, "新车用户数量超过最大限制");
                    }
                } else if (oldCar.size() > 0) {
                    if (oldCount == null) {
                        throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置二手车账号最大数量，请联系业务人员处理");
                    }
                    if (oldCarUserCount >= oldCount + 1) {
                        throw new AfsBaseException(CommonConstants.FAIL, "二手车用户数量超过最大限制");
                    }
                }

            }
        } else {
            throw new AfsBaseException(CommonConstants.FAIL, "渠道信息有误！");
        }

    }

    /**
     * 获取部门下区域信息
     *
     * @param username
     * @param businessType
     * @param parentId
     * @return
     */
    public List<SysDeptAuthRegion> getDeptAddressList(String username, CarTypeEnum businessType, String parentId) {

        return this.baseMapper.getDeptAddressList(username, businessType, parentId);
    }

    /**
     * 获取用户所有角色
     *
     * @param id
     * @return
     */
    @Override
    public List<SysRole> findRolesInfo(Long id) {
        return baseMapper.listRolesInfo(id);
    }

    /**
     * 同部门和同角色的用户
     *
     * @param condition
     * @return
     */
    public List<SysUser> getTransferUser(ApplyUserCondition condition,List<Long> ids) {

        return this.baseMapper.getTransferUser(condition,ids);
    }
    // 根据用户id 返回管理的角色id
    @Override
    public List<Long> getAdminId() {
        List<Long> ids =new ArrayList<>();
        List<String> code =new ArrayList<>();
        code.add(AfsEnumUtil.key(CreatUserEnum.ROLE_NEW_CHANNEL_ADMIN));
        code.add(AfsEnumUtil.key(CreatUserEnum.ROLE_OLD_CHANNEL_ADMIN));
        code.add(AfsEnumUtil.key(CreatUserEnum.ROLE_NEW_DIRECT_ADMIN));
        code.add(AfsEnumUtil.key(CreatUserEnum.ROLE_OLD_DIRECT_ADMIN));
        List<SysRole> role =applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getRoleCode,code));
        if(role.size()>0){
            role.forEach(en->{
                ids.add(en.getId());
            });
        }
        return ids;
    }

    // 根据用户id 返回新车管理的角色id
    @Override
    public List<Long> getNewAdminId() {
        List<Long> ids =new ArrayList<>();
        List<String> code =new ArrayList<>();
        code.add(AfsEnumUtil.key(CreatUserEnum.ROLE_NEW_CHANNEL_ADMIN));
        code.add(AfsEnumUtil.key(CreatUserEnum.ROLE_NEW_DIRECT_ADMIN));
        List<SysRole> role =applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getRoleCode,code));
        if(role.size()>0){
            role.forEach(en->{
                ids.add(en.getId());
            });
        }
        return ids;
    }


    // 根据用户id 返回二手车管理的角色id
    @Override
    public List<Long> getOldAdminId() {
        List<Long> ids =new ArrayList<>();
        List<String> code =new ArrayList<>();
        code.add(AfsEnumUtil.key(CreatUserEnum.ROLE_OLD_CHANNEL_ADMIN));
        code.add(AfsEnumUtil.key(CreatUserEnum.ROLE_OLD_DIRECT_ADMIN));
        List<SysRole> role =applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getRoleCode,code));
        if(role.size()>0){
            role.forEach(en->{
                ids.add(en.getId());
            });
        }
        return ids;
    }

    @Override
    public List<Long> getNewApply() {
        List<Long> ids =new ArrayList<>();
        List<String> code =new ArrayList<>();
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_XCRECORDED.name()));
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_XCAPPLY.name()));

        List<SysRole> role =applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getRoleCode,code));
        if(role.size()>0){
            role.forEach(en->{
                ids.add(en.getId());
            });
        }
        return ids;
    }

    @Override
    public List<Long> getOldApply() {
        List<Long> ids =new ArrayList<>();
        List<String> code =new ArrayList<>();
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_RECORDED.name()));
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_APPLY.name()));

        List<SysRole> role =applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getRoleCode,code));
        if(role.size()>0){
            role.forEach(en->{
                ids.add(en.getId());
            });
        }
        return ids;
    }

    @Override
    public List<Long> getNewRole() {
        List<Long> ids =new ArrayList<>();
        List<String> code =new ArrayList<>();
        // 新车预录单员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_XCRECORDED.name()));
        // 新车申报员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_XCAPPLY.name()));
        // 新车预防款员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_XCPREPAYMENT.name()));
        // 新车放款报单
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_XCPAYMENT.name()));

        List<SysRole> role =applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getRoleCode,code));
        if(role.size()>0){
            role.forEach(en->{
                ids.add(en.getId());
            });
        }
        return ids;
    }

    @Override
    public List<Long> getOldRole() {
        List<Long> ids =new ArrayList<>();
        List<String> code =new ArrayList<>();
        // 二手车预放款员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_PREPAYMENT.name()));
        // 二手车放款报单员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_PAYMENT.name()));
        // 二手车预录单员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_RECORDED.name()));
        // 二手车申请报单员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_APPLY.name()));

        List<SysRole> role =applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getRoleCode,code));
        if(role.size()>0){
            role.forEach(en->{
                ids.add(en.getId());
            });
        }
        return ids;
    }

    @Override
    public List<Long> getNewIdCarRoleId() {
        List<Long> ids =new ArrayList<>();
        List<String> code =new ArrayList<>();
        // 新车预防款员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_XCPREPAYMENT.name()));
        // 新车放款报单
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_XCPAYMENT.name()));

        List<SysRole> role =applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getRoleCode,code));
        if(role.size()>0){
            role.forEach(en->{
                ids.add(en.getId());
            });
        }
        return ids;
    }

    @Override
    public List<Long> getOldIdCarRoleId() {
        List<Long> ids =new ArrayList<>();
        List<String> code =new ArrayList<>();
        // 二手车预放款员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_PREPAYMENT.name()));
        // 二手车放款报单员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_PAYMENT.name()));
        // 二手车预录单员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_RECORDED.name()));
        // 二手车申请报单员
        code.add(AfsEnumUtil.key(RoleEnum.ROLE_APPLY.name()));

        List<SysRole> role =applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getRoleCode,code));
        if(role.size()>0){
            role.forEach(en->{
                ids.add(en.getId());
            });
        }
        return ids;
    }


    /**
     * 用户新增临时表相关校验
     *
     * @param userCondition
     * @return
     */
    public IResponse<Boolean> checkTemp(ApplyUserCondition userCondition) {

        // 获取这个部门能做新车还是二手车  还是都可以
        List<SysDeptAuthRegion> newCarRegion = applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda()
                .eq(DataAccessUtil.isAdmin(), SysDeptAuthRegion::getChannelId, userCondition.getChannelId())
                .eq(SysDeptAuthRegion::getDeptId, userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType, CarTypeEnum.newCar));

        List<SysDeptAuthRegion> oldCarRegion = applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda()
                .eq(DataAccessUtil.isAdmin(), SysDeptAuthRegion::getChannelId, userCondition.getChannelId())
                .eq(SysDeptAuthRegion::getDeptId, userCondition.getDeptId()).eq(SysDeptAuthRegion::getBusinessType, CarTypeEnum.oldCar));

        if (StrUtil.isBlank(userCondition.getUsername())) {
            throw new AfsBaseException(CommonConstants.FAIL, "用户名为空");
        }
        //  我第一步应该是拿到这个集合 然后 把 数据进行分类。分别为 ：新车  二手车  全有
        List<Long> newRoleIds=new ArrayList<>();
        List<Long> oldRoleIds =new ArrayList<>();
        List<Long> list = new ArrayList<>();
        if(userCondition.getNewRoleIds()!=null){
            newRoleIds= Arrays.asList(userCondition.getNewRoleIds());
            list.addAll(newRoleIds);
        }
        if(userCondition.getOldRoleIds()!=null){
            oldRoleIds= Arrays.asList(userCondition.getOldRoleIds());
            list.addAll(oldRoleIds);
        }
        // 查询所有我添加的角色信息
        List<SysRole> roleList = applyRoleService.list(Wrappers.<SysRole>query().lambda().in(SysRole::getId, list));
        // 放所有的我选择的角色的code 码
        List<String> roleAllList = new ArrayList<>();
        // 存放新车的code码
        List<String> newCarRoleList = new ArrayList<>();
        //存放二手车的code码
        List<String> oldCarRoleList = new ArrayList<>();
        // 这个应该是新车和二手车都有的code码
        List<String> carRoleList = new ArrayList<>();
        for (SysRole role : roleList) {
            roleAllList.add(role.getRoleCode());
            if (CarTypeEnum.newCar.name().equals(role.getRoleType())) {
                newCarRoleList.add(role.getRoleCode());
            } else if (CarTypeEnum.oldCar.name().equals(role.getRoleType())) {
                oldCarRoleList.add(role.getRoleCode());
            } else {
                carRoleList.add(role.getRoleCode());
            }
        }

        // 校验临时表用户数量
        chackSaveUserCountTemp(newCarRoleList, oldCarRoleList, carRoleList, newCarRegion, oldCarRegion,userCondition.getUserId());
        // 如果是添加 进去 修改不进去
        if (userCondition.getUserId() == null) {
            SysUserTemp applyUser = applyUserTempService.getOne(Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getUsername, userCondition.getUsername()));
            if (applyUser != null) {
                if (StatusEnum.NO.getValue().equals(applyUser.getLockFlag())) {
                    throw new AfsBaseException(CommonConstants.FAIL, "系统已存在此用户名用户，请不要重复创建！");
                } else if (StatusEnum.YES.getValue().equals(applyUser.getLockFlag())) {
                    throw new AfsBaseException(CommonConstants.FAIL, "系统已存在此用户名用户，为停用状态，请不要重复创建，请将原账号启用！");
                }
            }
        }

        if(roleAllList.contains(RoleEnum.ROLE_XCPREPAYMENT.name())){
            // 说明是新车放款那么举要看看他有没有另外
            if(!roleAllList.contains(RoleEnum.ROLE_XCRECORDED.name())){
                // 说明没有新车预录单员
                if(!roleAllList.contains(RoleEnum.ROLE_XCAPPLY.name())){
                    throw new AfsBaseException("新车预防款员必须拥有新车预录单员或者新车申请报单员！");
                }
            }
        }
        if(roleAllList.contains(RoleEnum.ROLE_PREPAYMENT.name())){
            // 说明是二手车放款员，那么要看看他有没有其他两个
            if(!roleAllList.contains(RoleEnum.ROLE_RECORDED.name())){
                if(!roleAllList.contains(RoleEnum.ROLE_APPLY.name())){
                    throw new AfsBaseException("二手车预防款员必须拥有二手车预录单员或者二手车申请报单员！");
                }
            }
        }


        if (roleAllList.contains(RoleEnum.ROLE_RECORDED.name()) && roleAllList.contains(RoleEnum.ROLE_APPLY.name())) {
            throw new AfsBaseException(CommonConstants.FAIL, "当前选择的角色预录单员角色与申请报单员角色不相容！");
        }
        if (roleAllList.contains(RoleEnum.ROLE_PAYMENT.name()) && roleAllList.contains(RoleEnum.ROLE_PREPAYMENT.name())) {
            throw new AfsBaseException(CommonConstants.FAIL, "当前选择的角色预放款角色与放款角色不相容！");
        }
        if (roleAllList.contains(RoleEnum.ROLE_XCRECORDED.name()) && roleAllList.contains(RoleEnum.ROLE_XCAPPLY.name())) {
            throw new AfsBaseException(CommonConstants.FAIL, "当前选择的角色新车预录单员角色与新车申请报单员角色不相容！");
        }
        if (roleAllList.contains(RoleEnum.ROLE_XCPAYMENT.name()) && roleAllList.contains(RoleEnum.ROLE_XCPREPAYMENT.name())) {
            throw new AfsBaseException(CommonConstants.FAIL, "当前选择的角色新车预放款角色与新车放款角色不相容！");
        }
        if (userCondition.getUserId() == null) {
            List<SysUserTemp> chanUser = applyUserTempService.list(Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getIdentityNumber, userCondition.getIdentityNumber()).eq(SysUserTemp::getChannelId, userCondition.getChannelId()));
            // 最多一个身份证在这个渠道有1次，如果有2次  说明这个这个身份证号搜索渠道管理员自己
            if (chanUser.size() <= 2) {
                for (SysUserTemp cuser : chanUser) {
                    if (StatusEnum.YES.getValue().equals(cuser.getLockFlag())) {
                        throw new AfsBaseException(CommonConstants.FAIL, "系统已存在相同身份证，为停用状态，请不要重复创建，请将原账号启用！");
                    }
                }
                if(chanUser.size()==2){
                    throw new AfsBaseException(CommonConstants.FAIL, "系统已存在相同身份证，请不要重复创建！");
                }
            } else {
                throw new AfsBaseException(CommonConstants.FAIL, "系统已存在相同身份证，请不要重复创建！");
            }
            //  当等于1的时候  要不是管理员要不就直接结束提示用户已存在
            if (chanUser.size() == 1) {
                // 判断这个身份证号是管理员的身份证号吗
                SysChannelBasicInfo basicInfo = applyChannelBasicInfoService.getOne(Wrappers.<SysChannelBasicInfo>query().lambda().eq(SysChannelBasicInfo::getChannelId, DataAccessUtil.getChannelId()));
                if (userCondition.getIdentityNumber().equals(basicInfo.getIdentityNumber())) {
                    // 说明这个人是管理员可以再创建一个

                } else {
                    // 说明不是管理员不可以创建了
                    if (StatusEnum.YES.getValue().equals(chanUser.get(0).getLockFlag())) {
                        throw new AfsBaseException(CommonConstants.FAIL, "系统已存在相同身份证，为停用状态，请不要重复创建，请将原账号启用！");
                    } else {
                        throw new AfsBaseException(CommonConstants.FAIL, "系统已存在相同身份证，请不要重复创建！");
                    }
                }
            }
        }

        // 再拿这个身份证号去判断在其它渠道有没有被启用的状态
        List<SysUserTemp> userList = applyUserTempService.list(Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getIdentityNumber, userCondition.getIdentityNumber()).eq(SysUserTemp::getLockFlag, StatusEnum.NO.getValue().toString()).eq(SysUserTemp::getDelFlag, StatusEnum.NO.getValue().toString()).notIn(SysUserTemp::getChannelId, userCondition.getChannelId().toString()));
        if (userList.size() > 0) {
            throw new AfsBaseException(CommonConstants.FAIL, "此身份证号已在其他渠道使用！");
        }
        // 下面是去校验见证人是不是见证人
        this.checkUserCard(userCondition.getIdentityNumber(), newCarRoleList, oldCarRoleList,userCondition.getChannelId());
        return IResponse.success(Boolean.TRUE);
    }
    private void chackSaveUserCountTemp(List<String> newCarRoleList, List<String> oldCarRoleList, List<String> carRoleList, List<SysDeptAuthRegion> newCar, List<SysDeptAuthRegion> oldCar,Long userId) {
        // 先查询 这个账户新车可以创建多少
        SysChannelBasicInfo one = applyChannelBasicInfoService.getOne(Wrappers.<SysChannelBasicInfo>lambdaQuery().eq(SysChannelBasicInfo::getChannelId, DataAccessUtil.getChannelId()));
        if (one != null) {
            // 获取这个合作商下面 新车和二手车被拒绝的用户（被拒绝的用户不参与校验）
            List<Long> newIdsTemp =new ArrayList<>();
            // 获取新车被拒绝的用户
            List<SysUserTemp> newUserTemp =applyUserTempService.list(Wrappers.<SysUserTemp>query().lambda().notIn(SysUserTemp::getNewStatus,StatusEnum.REFUSE.getValue().toString()));
            if(newUserTemp.size()>0){
                for (SysUserTemp temp:newUserTemp){
                    if(userId!=temp.getId()){
                        newIdsTemp.add(temp.getId());
                    }
                }
            }else {
                newIdsTemp=null;
            }
            List<Long> oldIdsTemp =new ArrayList<>();
            // 获取二手车被拒绝的用户
            List<SysUserTemp> oldUserTemp =applyUserTempService.list(Wrappers.<SysUserTemp>query().lambda().notIn(SysUserTemp::getOldStatus,StatusEnum.REFUSE.getValue().toString()));

            if(oldUserTemp.size()>0){
                for(SysUserTemp temp:oldUserTemp){
                    if(userId!=temp.getId()){
                        oldIdsTemp.add(temp.getId());
                    }
                }
            }
            // 声明一个新车 的用户数量
            Integer newCount = one.getNewCarUserHierarchy();
            Integer oldCount = one.getOldCarUserHierarchy();
            Integer newCarUserCount = baseMapper.findUserCountNotInUserTemp(CarTypeEnum.newCar.name(), DataAccessUtil.getChannelId(),newIdsTemp);
            Integer oldCarUserCount = baseMapper.findUserCountNotInUserTemp(CarTypeEnum.oldCar.name(), DataAccessUtil.getChannelId(),oldIdsTemp);

            // 查询这个渠道
            if (newCarRoleList.size() > 0) {
                if (newCount == null) {
                    throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置新车账号最大数量，请联系业务人员处理");
                }
                // 需要去判断新车的用户数量

                if (newCarUserCount >= newCount + 1) {
                    throw new AfsBaseException(CommonConstants.FAIL, "新车用户数量超过最大限制");
                }

            }
            if (oldCarRoleList.size() > 0) {
                if (oldCount == null) {
                    throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置二手车账号最大数量，请联系业务人员处理");
                }

                if (oldCarUserCount >= oldCount + 1) {
                    throw new AfsBaseException(CommonConstants.FAIL, "二手车用户数量超过最大限制");
                }
            }
            if (carRoleList.size() > 0) {
                // 这个就要看传递过来的区域是只有新车  还是只有二手车  还是都有
                if (newCar.size() > 0 && oldCar.size() > 0) {
                    if (newCount == null) {
                        throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置新车账号最大数量，请联系业务人员处理");
                    }
                    if (oldCount == null) {
                        throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置二手车账号最大数量，请联系业务人员处理");
                    }
                    if (newCarUserCount >= newCount + 1) {
                        throw new AfsBaseException(CommonConstants.FAIL, "新车用户数量超过最大限制");
                    }
                    if (oldCarUserCount >= oldCount + 1) {
                        throw new AfsBaseException(CommonConstants.FAIL, "二手车用户数量超过最大限制");
                    }
                } else if (newCar.size() > 0) {
                    if (newCount == null) {
                        throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置新车账号最大数量，请联系业务人员处理");
                    }
                    if (newCarUserCount >= newCount + 1) {
                        throw new AfsBaseException(CommonConstants.FAIL, "新车用户数量超过最大限制");
                    }
                } else if (oldCar.size() > 0) {
                    if (oldCount == null) {
                        throw new AfsBaseException(CommonConstants.FAIL, "渠道上线时未设置二手车账号最大数量，请联系业务人员处理");
                    }
                    if (oldCarUserCount >= oldCount + 1) {
                        throw new AfsBaseException(CommonConstants.FAIL, "二手车用户数量超过最大限制");
                    }
                }

            }
        } else {
            throw new AfsBaseException(CommonConstants.FAIL, "渠道信息有误！");
        }

    }
    @Override
    public void addOrUpUser(Long id,String carType,List<SysUserRole> applyUserRoleList,List<SysUserRoleTemp> applyUserRoleTempList,SysRole loan,SysRole brokerage, List<Long> loanAnd){

        // 查询新车 所有的进件角色
        List<Long> newIds =this.getNewApply();
        // 查询 二手车所有的进件角色
        List<Long> oldIds =this.getOldApply();
        // 查询出来这个用户临时表的信息
        SysUserTemp tempUser =applyUserTempService.getOne(Wrappers.<SysUserTemp>query().lambda().eq(SysUserTemp::getId,id));

        // 查询出来这个合作商的账户权限
        SysUserPower adminPower =sysUserPowerService.getOne(Wrappers.<SysUserPower>query().lambda().eq(SysUserPower::getChannelId,tempUser.getChannelId()).eq(SysUserPower::getUserManage,StatusEnum.YES.getValue().toString()));
        //  第一步先去看看这个用户有没有创建账户
        SysUser user=this.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUserId,id));
        if(user==null){
            // 说明我之前没有账户，我需要去创建账户。

            // 到这一部说明什么权限都符合了，那么就要去校验数据了
            ApplyUserCondition condition=new ApplyUserCondition();
            condition.setDeptId(tempUser.getDeptId());
            condition.setIdentityNumber(tempUser.getIdentityNumber());
            condition.setPhone(tempUser.getPhone());
            condition.setUserEmail(tempUser.getUserEmail());
            condition.setUserRealName(tempUser.getUserRealName());
            condition.setUsername(tempUser.getUsername());
            condition.setChannelId(tempUser.getChannelId());

            List<SysUserRoleTemp> roles =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getBusinessType,carType));
            List<Long> roleIds =new ArrayList<>();
            if(CarTypeEnum.newCar.toString().equals(carType)){
                // 说明是新车
                if(roles.size()>0){
                    for(SysUserRoleTemp roleId:roles){
                        roleIds.add(roleId.getRoleId());
                    }
                    Long[] newRoleIds =new Long[roleIds.size()];
                    for (int idx = 0; idx < roleIds.size(); idx++) {
                        newRoleIds[idx] = roleIds.get(idx);
                    }
                    condition.setNewRoleIds(newRoleIds);
                }else {
                    throw new AfsBaseException("获取用户新车角色失败！");
                }
            }else {
                if(roles.size()>0){
                    for(SysUserRoleTemp roleId:roles){
                        roleIds.add(roleId.getRoleId());
                    }
                    Long[] oldRoleIds =new Long[roleIds.size()];
                    for (int idx = 0; idx < roleIds.size(); idx++) {
                        oldRoleIds[idx] = roleIds.get(idx);
                    }
                    condition.setOldRoleIds(oldRoleIds);
                }else {
                    throw new AfsBaseException("获取用户二手车角色失败！");
                }

            }
            this.check(condition);

            // 校验通过后去创建数据
            SysUser u=new SysUser();
            u.setLockFlag(StatusEnum.NO.getValue());
            u.setLastLoginFailTime(0L);
            u.setLastPassChangeTime(0L);
            u.setPassErrorCount(0);
            u.setChannelId(tempUser.getChannelId());
            u.setUserId(tempUser.getId());
            u.setDeptId(tempUser.getDeptId());
            u.setIdentityNumber(tempUser.getIdentityNumber());
            u.setPhone(tempUser.getPhone());
            u.setUserRealName(tempUser.getUserRealName());
            u.setUserEmail(tempUser.getUserEmail());
            u.setUsername(tempUser.getUsername());
            //初始密码
            u.setPassword(SysUserService.ENCODER.encode(AfsEnumUtil.key(CreatUserEnum.DEFAULT_PWD)));
            if (!this.save(u)) {
                throw new AfsBaseException(CommonConstants.FAIL, "添加失败！");
            }

            // 去添加角色以及角色关系
            List<SysUserRole> sysUserRoles= JSONUtil.parseArray(roles).toList(SysUserRole.class);
            userRoleService.saveBatch(sysUserRoles);
            // 再看看这个集合有没有佣金和贷后，如果有那么就把新车二手车的贷后都给了
            for(SysUserRoleTemp temp:roles){
                if(temp.getRoleId().toString().equals(loan.getId().toString())){
                    // 说明我有贷后的角色
                    SysUserRole role=new SysUserRole();
                    if(CarTypeEnum.newCar.toString().equals(carType)){
                        // 说明是新车那么我就要赋值二手车
                        role.setBusinessType(CarTypeEnum.oldCar);
                    }else {
                        role.setBusinessType(CarTypeEnum.newCar);
                    }
                    role.setUserId(id);
                    role.setRoleId(temp.getRoleId());
                    userRoleService.save(role);
                }
                if(temp.getRoleId().toString().equals(brokerage.getId().toString())){
                    // 说明我有佣金角色
                    SysUserRole role=new SysUserRole();
                    if(CarTypeEnum.newCar.toString().equals(carType)){
                        // 说明是新车那么我就要赋值二手车
                        role.setBusinessType(CarTypeEnum.oldCar);
                    }else {
                        role.setBusinessType(CarTypeEnum.newCar);
                    }
                    role.setUserId(id);
                    role.setRoleId(brokerage.getId());
                    userRoleService.save(role);
                }
            }

            // 我要赋值权限表
            // 新车进件权限
            List<SysUserRole> applyUserNewRole =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,u.getUserId()).in(SysUserRole::getRoleId,newIds));
            // 二手车进件权限
            List<SysUserRole> applyUserOldRole =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,u.getUserId()).in(SysUserRole::getRoleId,oldIds));
            // 新车账号权限
            List<SysUserRole> newUser =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,u.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.newCar.toString()));
            // 二手车账号权限
            List<SysUserRole> oldUser =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,u.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.oldCar.toString()));

            //部门新车权限
            List<SysDeptAuthRegion> newDept=applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda().eq(SysDeptAuthRegion::getDeptId,u.getDeptId()).eq(SysDeptAuthRegion::getBusinessType,CarTypeEnum.newCar.toString()));
            // 部门二手车权限
            List<SysDeptAuthRegion> oldDept=applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda().eq(SysDeptAuthRegion::getDeptId,u.getDeptId()).eq(SysDeptAuthRegion::getBusinessType,CarTypeEnum.oldCar.toString()));

            // 去查这个用户的权限
            SysUserPower power=sysUserPowerService.getOne(Wrappers.<SysUserPower>query().lambda().eq(SysUserPower::getUserId,u.getUserId()));
            // 这边去判断用户的权限
            if(power==null){
                power =new SysUserPower();
                power.setUserId(u.getUserId());
                power.setChannelId(u.getChannelId());
                // 说明有新车
                if(applyUserNewRole.size()>0){
                    if(adminPower.getNewApplyPower().toString().equals(StatusEnum.NO.getValue().toString())){
                        power.setNewApplyPower(StatusEnum.NO.getValue());
                    }else {
                        throw new AfsBaseException("暂无新车进件权限！");
                    }
                }else {
                    power.setNewApplyPower(StatusEnum.NOTHING.getValue());
                }
                if(newUser.size()>0){
                    if(newDept.size()>0){
                        if(adminPower.getNewAccountPower().toString().equals(StatusEnum.NO.getValue().toString())){
                            power.setNewAccountPower(StatusEnum.NO.getValue());
                        }else {
                            throw new AfsBaseException("暂无新车账户权限！");
                        }
                    }else {
                        power.setNewAccountPower(StatusEnum.NOTHING.getValue());
                    }

                }else {
                    power.setNewAccountPower(StatusEnum.NOTHING.getValue());
                }
                if(applyUserOldRole.size()>0){
                    if(adminPower.getOdlApplyPower().toString().equals(StatusEnum.NO.getValue().toString())){
                        power.setOdlApplyPower(StatusEnum.NO.getValue());
                    }else {
                        throw new AfsBaseException("暂无二手车进件权限！");
                    }
                }else {
                    power.setOdlApplyPower(StatusEnum.NOTHING.getValue());
                }
                if(oldUser.size()>0){
                    if(oldDept.size()>0){
                        if(adminPower.getOldAccountPower().toString().equals(StatusEnum.NO.getValue().toString())){
                            power.setOldAccountPower(StatusEnum.NO.getValue());
                        }else {
                            throw new AfsBaseException("暂无二手车账户权限！");
                        }
                    }else {
                        power.setOldAccountPower(StatusEnum.NOTHING.getValue());
                    }

                }else {
                    power.setOldAccountPower(StatusEnum.NOTHING.getValue());
                }
                power.setLockFlag(StatusEnum.NO.getValue());
                sysUserPowerService.save(power);
                log.info("==================================看看用户的id==================================》"+u.getUserId().toString());
                // add by mingzhi.li 20201010 11:06:26 用户上线同步归档系统
                try{
                    Map<String, String> requestHeader = new HashMap<>();
                    requestHeader.put("clientId", applyAdminConfig.getChannelClientId());
                    requestHeader.put("clientSecret", applyAdminConfig.getChannelClientSecret());
                    ArchiveUserInfoVo vo = new ArchiveUserInfoVo();
                    vo.setUserId(u.getUserId().toString());
                    applyAdminUseChannelFeign.sendApplyUser(vo,requestHeader);
                }catch (Exception e){
                    log.error("用户上线同步归档异常");
                }
                // add by mingzhi.li 20201010 11:06:26 用户上线同步归档系统
            }else {
                throw new AfsBaseException("添加进件权限信息有误！");
            }

        }else {
            // 说明我已经存在正式的用户


            // 修改数据
            user.setDeptId(tempUser.getDeptId());
            user.setIdentityNumber(tempUser.getIdentityNumber());
            user.setPhone(tempUser.getPhone());
            user.setUserRealName(tempUser.getUserRealName());
            user.setUserEmail(tempUser.getUserEmail());
            user.setUsername(tempUser.getUsername());
            user.setLockFlag(StatusEnum.NO.getValue());
            this.update(user,Wrappers.<SysUser>query().lambda().eq(SysUser::getUserId,id));

            ApplyUserCondition condition=new ApplyUserCondition();
            condition.setUserId(tempUser.getId());
            condition.setDeptId(tempUser.getDeptId());
            condition.setIdentityNumber(tempUser.getIdentityNumber());
            condition.setPhone(tempUser.getPhone());
            condition.setUserEmail(tempUser.getUserEmail());
            condition.setUserRealName(tempUser.getUserRealName());
            condition.setUsername(tempUser.getUsername());
            condition.setChannelId(tempUser.getChannelId());
            // 先移除所有的现有本类型的角色
            applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getBusinessType,carType));
            // 再移除佣金贷后
            applyUserRoleTempService.remove(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).in(SysUserRoleTemp::getRoleId,loanAnd));

            // 再把原来的角色属性还回去，记得佣金贷后新车二手车都给
            if(applyUserRoleTempList==null){
                throw new AfsBaseException("原审批数据异常");
            }
            if(applyUserRoleTempList.size()>0){
                for (SysUserRoleTemp roleTemp:applyUserRoleTempList){
                    SysUserRoleTemp temp=new SysUserRoleTemp();
                    temp.setBusinessType(roleTemp.getBusinessType());
                    temp.setUserId(roleTemp.getUserId());
                    temp.setRoleId(roleTemp.getRoleId());
                    applyUserRoleTempService.save(temp);
                        // 判断有没有佣金贷后
                        if(loan.getId().toString().equals(roleTemp.getRoleId().toString())||brokerage.getId().toString().equals(roleTemp.getRoleId().toString())){
                            // 需要给佣金贷后追加双重属性
                            SysUserRoleTemp tempTemp=new SysUserRoleTemp();
                            if(CarTypeEnum.newCar.toString().equals(roleTemp.getBusinessType().toString())){
                                tempTemp.setBusinessType(CarTypeEnum.oldCar);
                            }else {
                                tempTemp.setBusinessType(CarTypeEnum.newCar);
                            }
                            tempTemp.setUserId(roleTemp.getUserId());
                            tempTemp.setRoleId(roleTemp.getRoleId());
                            applyUserRoleTempService.save(tempTemp);
                        }

                }
            }else {
                throw new AfsBaseException("原审批数据异常");
            }

            List<SysUserRoleTemp> roles =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,tempUser.getId()).eq(SysUserRoleTemp::getBusinessType,carType));
            List<Long> roleIds =new ArrayList<>();
            if(CarTypeEnum.newCar.toString().equals(carType)){
                // 说明是新车
                if(roles.size()>0){
                    for(SysUserRoleTemp roleId:roles){
                        roleIds.add(roleId.getRoleId());
                    }
                    Long[] newRoleIds =new Long[roleIds.size()];
                    for (int idx = 0; idx < roleIds.size(); idx++) {
                        newRoleIds[idx] = roleIds.get(idx);
                    }
                    condition.setNewRoleIds(newRoleIds);
                }else {
                    throw new AfsBaseException("获取用户新车角色失败！");
                }
            }else {
                if(roles.size()>0){
                    for(SysUserRoleTemp roleId:roles){
                        roleIds.add(roleId.getRoleId());
                    }
                    Long[] oldRoleIds =new Long[roleIds.size()];
                    for (int idx = 0; idx < roleIds.size(); idx++) {
                        oldRoleIds[idx] = roleIds.get(idx);
                    }
                    condition.setOldRoleIds(oldRoleIds);
                }else {
                    throw new AfsBaseException("获取用户二手车角色失败！");
                }

            }
            this.check(condition);

            // 移除原来的用户这个属性的角色
            userRoleService.remove(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,user.getUserId()).eq(SysUserRole::getBusinessType,carType));
            // 移除关于佣金贷后的数据
            userRoleService.remove(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,user.getUserId()).in(SysUserRole::getRoleId,loanAnd));

            // 去添加角色以及角色关系
            List<SysUserRole> sysUserRoles= JSONUtil.parseArray(roles).toList(SysUserRole.class);
            userRoleService.saveBatch(sysUserRoles);
            // 再看看这个集合有没有佣金和贷后，如果有那么就把新车二手车的贷后都给了
            for(SysUserRoleTemp temp:roles){
                if(temp.getRoleId().toString().equals(loan.getId().toString())){
                    // 说明我有贷后的角色
                    SysUserRole role=new SysUserRole();
                    if(CarTypeEnum.newCar.toString().equals(carType)){
                        // 说明是新车那么我就要赋值二手车,但是要先查看下有没有二手车
                        List<SysUserRole> seeRole =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getRoleId,temp.getRoleId()).eq(SysUserRole::getUserId,user.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.oldCar.toString()));
                        if(seeRole.size()<=0){
                            role.setBusinessType(CarTypeEnum.oldCar);
                            role.setUserId(id);
                            role.setRoleId(temp.getRoleId());
                            userRoleService.save(role);
                        }
                    }else {
                        List<SysUserRole> seeRole =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getRoleId,temp.getRoleId()).eq(SysUserRole::getUserId,user.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.newCar.toString()));
                        if(seeRole.size()<=0){
                            role.setBusinessType(CarTypeEnum.newCar);
                            role.setUserId(id);
                            role.setRoleId(temp.getRoleId());
                            userRoleService.save(role);
                        }
                    }

                }
                if(temp.getRoleId().toString().equals(brokerage.getId().toString())){
                    // 说明我有佣金角色
                    SysUserRole role=new SysUserRole();
                    if(CarTypeEnum.newCar.toString().equals(carType)){
                        // 说明是新车那么我就要赋值二手车,但是要先查看下有没有二手车
                        List<SysUserRole> seeRole =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getRoleId,temp.getRoleId()).eq(SysUserRole::getUserId,user.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.oldCar.toString()));
                        if(seeRole.size()<=0){
                            role.setBusinessType(CarTypeEnum.oldCar);
                            role.setUserId(id);
                            role.setRoleId(temp.getRoleId());
                            userRoleService.save(role);
                        }
                    }else {
                        List<SysUserRole> seeRole =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getRoleId,temp.getRoleId()).eq(SysUserRole::getUserId,user.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.newCar.toString()));
                        if(seeRole.size()<=0){
                            role.setBusinessType(CarTypeEnum.newCar);
                            role.setUserId(id);
                            role.setRoleId(temp.getRoleId());
                            userRoleService.save(role);
                        }
                    }
                }
            }
            // 最后看下部门
            //部门新车权限
            List<SysDeptAuthRegion> newDept=applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda().eq(SysDeptAuthRegion::getDeptId,user.getDeptId()).eq(SysDeptAuthRegion::getBusinessType,CarTypeEnum.newCar.toString()));
            // 部门二手车权限
            List<SysDeptAuthRegion> oldDept=applyDeptAuthRegionService.list(Wrappers.<SysDeptAuthRegion>query().lambda().eq(SysDeptAuthRegion::getDeptId,user.getDeptId()).eq(SysDeptAuthRegion::getBusinessType,CarTypeEnum.oldCar.toString()));
            if(newDept.size()>0&&oldDept.size()>0){

            }else {
                // 清除现在所有用户的角色，然后重新来过
                userRoleService.remove(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,user.getUserId()));
                // 把临时表数值直接赋值给他
                List<SysUserRoleTemp> roleTemps =applyUserRoleTempService.list(Wrappers.<SysUserRoleTemp>query().lambda().eq(SysUserRoleTemp::getUserId,user.getUserId()));
                for(SysUserRoleTemp role :roleTemps){
                    SysUserRole userRole =new SysUserRole();
                    userRole.setUserId(role.getUserId());
                    userRole.setBusinessType(role.getBusinessType());
                    userRole.setRoleId(role.getRoleId());
                    userRoleService.save(userRole);
                }

            }

            // 新车进件权限
            List<SysUserRole> applyUserNewRole =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,user.getUserId()).in(SysUserRole::getRoleId,newIds));
            // 二手车进件权限
            List<SysUserRole> applyUserOldRole =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,user.getUserId()).in(SysUserRole::getRoleId,oldIds));
            // 新车账号权限
            List<SysUserRole> newUser =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,user.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.newCar.toString()));
            // 二手车账号权限
            List<SysUserRole> oldUser =userRoleService.list(Wrappers.<SysUserRole>query().lambda().eq(SysUserRole::getUserId,user.getUserId()).eq(SysUserRole::getBusinessType,CarTypeEnum.oldCar.toString()));





            //我要修改权限表
            SysUserPower power=sysUserPowerService.getOne(Wrappers.<SysUserPower>query().lambda().eq(SysUserPower::getUserId,user.getUserId()));
            if(power==null){
                throw new AfsBaseException("获取用户权限失败！");
            }else {
                // 说明有新车
                if(applyUserNewRole.size()>0){
                    if(adminPower.getNewApplyPower().toString().equals(StatusEnum.NO.getValue().toString())){
                        if(StatusEnum.YES.getValue().toString().equals(power.getNewApplyPower())){
                            throw new AfsBaseException("暂无新车进件权限！");
                        }else {
                            power.setNewApplyPower(StatusEnum.NO.getValue());
                        }
                    }else {
                        throw new AfsBaseException("暂无新车进件权限！");
                    }
                }else {
                    power.setNewApplyPower(StatusEnum.NOTHING.getValue());
                }
                if(newUser.size()>0){
                    if(newDept.size()>0){
                        if(adminPower.getNewAccountPower().toString().equals(StatusEnum.NO.getValue().toString())){
                            if(StatusEnum.YES.getValue().toString().equals(power.getNewAccountPower())){
                                throw new AfsBaseException("用户暂无新车账户权限！");
                            }else {
                                power.setNewAccountPower(StatusEnum.NO.getValue());
                            }
                        }else {
                            throw new AfsBaseException("暂无新车账户权限！");
                        }
                    }else {
                        power.setNewAccountPower(StatusEnum.NOTHING.getValue());
                    }

                }else {
                    power.setNewAccountPower(StatusEnum.NOTHING.getValue());
                }
                if(applyUserOldRole.size()>0){
                    if(adminPower.getOdlApplyPower().toString().equals(StatusEnum.NO.getValue().toString())){
                        if(StatusEnum.YES.getValue().toString().equals(power.getOdlApplyPower())){
                            throw new AfsBaseException("暂无二手车进件权限！");
                        }else {
                            power.setOdlApplyPower(StatusEnum.NO.getValue());
                        }
                    }else {
                        throw new AfsBaseException("暂无二手车进件权限！");
                    }
                }else {
                    power.setOdlApplyPower(StatusEnum.NOTHING.getValue());
                }
                if(oldUser.size()>0){
                    if(oldDept.size()>0){
                        if(adminPower.getOldAccountPower().toString().equals(StatusEnum.NO.getValue().toString())){
                            if(StatusEnum.YES.getValue().toString().equals(power.getOldAccountPower())){
                                throw new AfsBaseException("用户暂无二手车账户权限！");
                            }else {
                                power.setOldAccountPower(StatusEnum.NO.getValue());
                            }
                        }else {
                            throw new AfsBaseException("暂无二手车账户权限！");
                        }
                    }else {
                        power.setOldAccountPower(StatusEnum.NOTHING.getValue());
                    }

                }else {
                    power.setOldAccountPower(StatusEnum.NOTHING.getValue());
                }
                sysUserPowerService.update(power,Wrappers.<SysUserPower>query().lambda().eq(SysUserPower::getUserId,user.getUserId()));
            }


        }
    }

    private static boolean checkDiffrent(List<Long> list, List<Long> list1) {
        long st = System.nanoTime();
        if(list.size() != list1.size()) {

            return false;
        }
        for(Long str : list) {
            if(!list1.contains(str)) {

                return false;
            }
        }
        System.out.println("消耗时间：" + (System.nanoTime() - st));
        return true;
    }

}
